WordPress.org

Ready to get started?Download WordPress

Forums

Change default query using request filter (7 posts)

  1. charliebrimm
    Member
    Posted 2 years ago #

    I working on a site with heavy traffic so expensive queries are avoided by not using query_posts since it tosses the orig query. I need a way to filter the request to alter the query before it fires and make it only apply to only a category and not interfere with other pages. I'm using something similar to:

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

    The problem is that this query runs for every archive page (which interferes with search page and other pages). I need a way to make it only apply to the category.php page and then a way to filter for archive.php and others. In this case is_category and is_archive do not work.

    Any suggestions are appreciated.

  2. vtxyzzy
    Member
    Posted 2 years ago #

    Ome method would be to set a global variable just before the query where the filter is to be active and clear it just after the query. Then check for the global inside the filter function.

  3. charliebrimm
    Member
    Posted 2 years ago #

    That's not a bad idea, but where would I put that variable? The function and filter are added to functions.php so that it gets run before the original query has fired so that's a tough one.

    I also need to do this for another query that is fired on the main index page of the site as well and a filter to alter that query to something else.

  4. vtxyzzy
    Member
    Posted 2 years ago #

    The filter is fired only when a query is submitted, not when functions.php is processed. If the filter is to be used in a template without a specific query_posts() call, you will need to repeat the call so you can set/clear the global around it.

    You should be able to repeat the same query with this:

    global $wp_query, $my_filter_flag;
    $my_filter_flag = true;
    query_posts( $wp_query->query );
    $my_filter_flag = false;
  5. charliebrimm
    Member
    Posted 2 years ago #

    That makes sense. So you are saying to put this in the template file itself? Or somewhere else?

    Does this cause the original query to get tossed away and then the query is rerun?

  6. vtxyzzy
    Member
    Posted 2 years ago #

    Yes the code goes before the start of the Loop in the template file. The original query is just rerun and should give the same results.

  7. charliebrimm
    Member
    Posted 2 years ago #

    Hmm. That's a bummer because I was trying to avoid running the query twice which is why I added it as a filter. BTW this is a network site that has 10 blogs and there are many instances where I am using switch_to_blog to get posts from the main blog. I'm trying to keep the queries down because this will be a pretty high traffic site inside an enterprise.

Topic Closed

This topic has been closed to new replies.

About this Topic