0

I want to exclude the last post of the category "prima pagina" from the loop of my home page because I've got a dedicated box for it and I don't want duplicates on the page. I'm using a child-theme but I don't want to override the index.php so I've added this code to my functions.php

function cdbz_modify_main_query( $query ) {
    global $post;
    $exclude = [];
    $fArgs = array( 
        'category_name' => 'prima-pagina',
        'posts_per_page' => 1,
        'ignore_sticky_posts' => 1,
        'fields' => 'ids'
    );
    $featured = new WP_Query($fArgs);
    foreach($featured as $fId) { $exclude[] = $fId->ID;};
    wp_reset_query();
    if ( !is_admin() && $query->is_home() && $query->is_main_query() ) { 
      $query->set('post__not_in', $exclude); 
    }
}
add_action( 'pre_get_posts', 'cdbz_modify_main_query' );

It breaks the site. There are problem with the WP_query part but I don't understand why. If I put the same query inside another page of the template it works fine (it returns one id). Any help will be very appreciated. Thanks.

2

Couple of issues here

  • You custom query should be inside your conditional statement

  • Use get_posts which only returns the $posts property

  • wp_reset_query() is used with query_posts which you must never ever use

  • No need to the $post global

Your code should look something like this

add_action( 'pre_get_posts', 'cdbz_modify_main_query' );
function cdbz_modify_main_query( $query ) 
{
    if (    !is_admin() 
         && $query->is_home() 
         && $query->is_main_query() 
    ) {
            $fArgs = array( 
                'category_name' => 'prima-pagina',
                'posts_per_page' => 1,
                'ignore_sticky_posts' => 1,
                'fields' => 'ids'
            );
            $featured = get_posts( $fArgs );

            if ( $featured )
                $query->set( 'post__not_in', $featured ); 
    }
}

EDIT

From comments, you need to know why the custom query needs to be inside your conditional statement. The reason for this is, you would only want to run the custom query when we are on the home page and only for the main query. As your code stands, you are running the custom query on every page load and for all custom queries, and even on the back end, regardless. This is unnecessary and a waste of resources.

I hope that makes sense

| improve this answer | |
  • This works, thanks! I don't understand why I need to put the query featured inside the if statement. Can you please explain? – Ziqurrat Sep 10 '15 at 11:51
  • Checked! You're right, I don't need the featured query everywhere. It was a silly question :) my real problem was how to get the array of ids (only one value in this case) from the WP_Query I think I need to learn more about the use of the queries. Thanks – Ziqurrat Sep 10 '15 at 12:38
  • No question is ever silly :-). get_posts uses WP_Query but only returns the $posts property from the query object. Also to note, get_posts skips pagination, so it is faster than WP_Query by default. So, get_posts is just convenient in such situations. :-) Enjoy – Pieter Goosen Sep 10 '15 at 12:43
  • Just one extra note, to acces your posts in your code in question, you should use $featured->posts. This is exactly what get_posts do. – Pieter Goosen Sep 10 '15 at 12:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.