0

I'm trying to query the main loop on my home.php template to display posts which are only in a specific taxonomy 'issue-41'. I'm using the pre_get_posts function to achieve this but when testing it, all the sticky posts are displaying at the top before the query kicks in.

If i add $query->set( 'ignore_sticky_posts', 1 ); the query works but this also removes the sticky post from the top of the posts I'm trying to target.

Is there a way I can use pre_get_posts on the home.php to only shows posts from issue-41 and ensure the sticky post set in issue-41 remains at the top?

 function uk_market_revew_latest_issue($query) {
    if ( ! is_admin() || ! is_single() ) {
         if($query->is_home() && $query->is_main_query()) {
             $issues = get_terms(array('taxonomy' => 'issue', 'hide_empty' => true));
             $ids = array();

             foreach($issues as $issue) {
                 $ids[] = $issue->slug;
             }

             $format = str_replace('issue-', '', $ids);
             $latest = max($format);

             print_r($latest);

             $tax_query = array(
                 array (
                     'taxonomy' => 'issue',
                     'field' => 'slug',
                     'terms' => 'issue-41',
                     'operator' => 'IN'
                )
            );

             $query->set('tax_query', $tax_query);
             $query->set('posts_per_page', '-1');
             $query->set( 'ignore_sticky_posts', 1 );
         }
     }
}

 add_action( 'pre_get_posts', 'uk_market_revew_latest_issue' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.