• Resolved openbayou

    (@openbayou)


    I’m using two pre_get_posts to set two queries. I would like to have one query but don’t know if it’s possible.

    function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
      $query->set('tax_query', array(array('taxonomy' => 'category','field' => 'slug','terms' => array( 'podcast-control-daily' ),'operator'=> 'NOT IN'),array('taxonomy' => 'post_format','field' => 'slug','terms' => array( 'post-format-status' ),'operator'=> 'NOT IN')));
      $query->set( 'posts_per_page', 15 );
    }
    return $query;}
    add_action( 'pre_get_posts', 'exclude_category' );
    function ctrl_daily_posts( $query ){
          if( $query->get( 'ctrl_daily' ) == 'posts' ){
            $query->set( 'tax_query', array(array('taxonomy' => 'category','field' => 'slug','terms' => array( 'podcast-control-daily' ),'operator'=> 'IN')));
            $query->set( 'posts_per_page', 5 );
          }
    }
    add_action( 'pre_get_posts', 'ctrl_daily_posts', 10 );
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    sure, but I think keeping them separate will make it easier to figure out in six months when you come back to the site to fix something. the logic seems cleaner this way.

    Thread Starter openbayou

    (@openbayou)

    So it’s easier to have two queries instead of trying to get two queries into the main query?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I didn’t say that. You could combine them, but I think the logic would look messy. I’m not sure what problem you’re trying to solve with your question.

    Thread Starter openbayou

    (@openbayou)

    I don’t know at this point. I’ve been working on this for so much that I’m probably over-thinking my problem and I probably made it more complex that it’s suppose to be.

    Basically, I’m trying to find a way to have one query then separate posts based on settings. For example, one query has 15 posts but stripped out posts from the category podcast-control-daily and then another query with posts that are in category podcast-control-daily and put those back into the main_query. What happens is sometimes those posts with a category podcast-control-daily get pushed to the bottom and are no longer shown on the front page.

    The code above does work, my only problem with it is that it does two queries and I would like it to be one.

    Thread Starter openbayou

    (@openbayou)

    In other words, is there a way take this query:

    add_action( 'pre_get_posts', 'exclude_category' );
    function ctrl_daily_posts( $query ){
          if( $query->get( 'ctrl_daily' ) == 'posts' ){
            $query->set( 'tax_query', array(array('taxonomy' => 'category','field' => 'slug','terms' => array( 'podcast-control-daily' ),'operator'=> 'IN')));
            $query->set( 'posts_per_page', 5 );
          }
    }
    add_action( 'pre_get_posts', 'ctrl_daily_posts', 10 );

    and add it to the main query on top of this being already set:

    function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
      $query->set('tax_query', array(array('taxonomy' => 'category','field' => 'slug','terms' => array( 'podcast-control-daily' ),'operator'=> 'NOT IN'),array('taxonomy' => 'post_format','field' => 'slug','terms' => array( 'post-format-status' ),'operator'=> 'NOT IN')));
      $query->set( 'posts_per_page', 15 );
    }
    return $query;}
    add_action( 'pre_get_posts', 'exclude_category' );
    Thread Starter openbayou

    (@openbayou)

    Never mind.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Combine pre_get_posts into one query’ is closed to new replies.