WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Determine if category.php is called BEFORE query fires

[Resolved] Determine if category.php is called BEFORE query fires

  • Is it possible to do this? I need to put a conditional query in functions.php for only categories and do not want to use query_posts.

    global $template is null in functions.php

Viewing 11 replies - 1 through 11 (of 11 total)
  • esmi

    @esmi

    Forum Moderator

    You should be able to use the is_category conditional.

    I forgot to mention that I tried that. This causes a conflict with other pages that are of the same type archive.php is affected as well as other pages on the site so I want to single out category.php only. is_category doesn’t work unfortunately.

    esmi

    @esmi

    Forum Moderator

    is_category() will only return true for category pages – not general archive page. Perhaps you need to create a category.php template file?

    I think I had it in the wrong place. On your suggestion I reapplied and it worked fine. I must’ve put it in the wrong place previously. Thanks!

    esmi

    @esmi

    Forum Moderator

    No problem 🙂

    Hmm still not working. Perhaps you can see what is going on.

    function alter_the_query( $request ) {
    
    	global $blog_id;
    
    	if ($blog_id == 1 && is_category()) {
    		if (is_category()) {
    		switch_to_blog(10);
    		} else {
    		restore_current_blog();
    		}
    	}
        $new_cat_query = new WP_Query();
        $new_cat_query->parse_query( $request );
    
        return $request;
    }
    add_filter( 'request', 'alter_the_query' );

    if I put !is_category the query fires but it breaks other pages and the query doesn’t fire as written.

    esmi

    @esmi

    Forum Moderator

    What is it that you trying to do?

    I’m trying to externalize the query to functions.php to get it to query from another blog in a multisites without using query posts because the pagination doesn’t work correctly inside category.php. The switch_to_blog makes it query another table. I am trying to display categories from another blog where posts from all blogs are stored. Without the if of is_category the query runs fine and everything works as expected but it causes other queries to fail like multisite search and login pages which are all redirected to blog 1.

    Hope that explains it clearly.

    esmi

    @esmi

    Forum Moderator

    I think you need to post this in the Multisite forum.

    This worked! I was putting the switch in the wrong place so it was firing regardless>

    function alter_the_query( $request ) {
        $new_cat_query = new WP_Query();  // the query isn't run if we don't pass any query vars
        $new_cat_query->parse_query( $request );
    
        // this is the actual manipulation; do whatever you need here
        if ( $new_cat_query->is_category()  ) {
    		switch_to_blog(10);
    	}
        return $request;
    }
    add_filter( 'request', 'alter_the_query' );

    Thanks again for your time.

    esmi

    @esmi

    Forum Moderator

    Glad to hear that you got it sorted. 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘[Resolved] Determine if category.php is called BEFORE query fires’ is closed to new replies.
Skip to toolbar