I'm displaying posts from a category and want to show all but the first one using posts__not_in. The code below is returning a property of non object error.

function modify_onthisday_cat( $query ) {

    if ($query->is_category(7) && $query->is_main_query() ) {

        global $post, $wp_query;

        set_query_var( 'day', date('d') );
        set_query_var( 'post__not_in', array($post->ID) );

add_action( 'pre_get_posts', 'modify_onthisday_cat');

If I replace $post->ID with a specific post id, it will work, but I need it to be dynamic. From what I understand this action is ran before the query so getting that id dynamically is going to take some more work. I'm just not sure how to get there.

Edit (clarification):

Currently, I have a category page served up with the category.php template. I have a featured post displayed above and the remaining posts displayed below. The featured post is called from a custom function and the remaining posts are called from the default wordpress loop.

My goal is to show the posts below while not showing the first post and preserving pagination.

  • You want to exclude the first posts from that category page ? And while filtering the main query, this is logical that the $post will not be available. What is the goal actually ?
    – Shazzad
    Mar 5, 2014 at 20:30

1 Answer 1


You are using pre_get_posts incorrectly.

  1. You should not be returning anything at all
  2. And you should be manipulating the query object that is passed into the filter and not modifying the globals at all.

For example:

function modify_onthisday_cat( $query ) {
  if ($query->is_category(7) && $query->is_main_query() ) {
    global $post; // not going to be set yet
    $query->set( 'day', date('d') );
    $query->set( 'post__not_in', array($post->ID) ); // and this will fail
add_action( 'pre_get_posts', 'modify_onthisday_cat');

As noted in a comment, though, this won't work for the main query (and you seem to intend it for such), since $post won't be populated until after the main query runs-- that is what determines the value(s) in $post. I think you are going to have to explain your goal in more detail.

Something like the following should work better for single post pages, at least.

function modify_onthisday_cat( $query ) {
  $post_id = get_query_var('p');
  if ($query->is_category(7) && $query->is_main_query() && !empty($post_id)) {
    $query->set( 'day', date('d') );
    $query->set( 'post__not_in', array($post_id) );
add_action( 'pre_get_posts', 'modify_onthisday_cat');
  • Turns out I was over thinking it. A simple set_query_var( 'offset', 1 ); did the trick. Thanks. Mar 5, 2014 at 21:14
  • @certainstrings Then please mark the answer as solution by ticking the checkmark below the voting arrows on the left hand side of this question
    – kaiser
    Mar 5, 2014 at 21:48
  • @certainstrings: I am not convinced that manipulating offset is a reliable fix.
    – s_ha_dum
    Mar 5, 2014 at 21:53
  • You are correct. It disables category pagination when doing so. I'll have to troubleshoot another fix. Mar 6, 2014 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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