WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Determine if category.php is called BEFORE query fires (12 posts)

  1. charliebrimm
    Member
    Posted 2 years ago #

    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

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    You should be able to use the is_category conditional.

  3. charliebrimm
    Member
    Posted 2 years ago #

    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.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

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

  5. charliebrimm
    Member
    Posted 2 years ago #

    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!

  6. esmi
    Forum Moderator
    Posted 2 years ago #

    No problem :-)

  7. charliebrimm
    Member
    Posted 2 years ago #

    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.

  8. esmi
    Forum Moderator
    Posted 2 years ago #

    What is it that you trying to do?

  9. charliebrimm
    Member
    Posted 2 years ago #

    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.

  10. esmi
    Forum Moderator
    Posted 2 years ago #

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

  11. charliebrimm
    Member
    Posted 2 years ago #

    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.

  12. esmi
    Forum Moderator
    Posted 2 years ago #

    Glad to hear that you got it sorted. :-)

Topic Closed

This topic has been closed to new replies.

About this Topic