0

I want to do something like this but can't manage to figure it out. [Click][1]

I am using this custom WP_Query to display upcoming events on my homepage.

        $events_args = array(
        'post_type' => VA_EVENT_PTYPE,
        'posts_per_page' => $number,
        'no_found_rows' => true,
        'post_status' => 'publish',
        'ignore_sticky_posts' => true,
        'tax_query' => array(
            array(
                'taxonomy' => VA_EVENT_DAY,
                'field' => 'slug',
                'terms' => $days,
                'include_children' => false,
            ),
        ),
        'meta_key' => VA_EVENT_DATE_META_KEY,
        'orderby' => 'meta_value',
        'order' => 'asc'
    );

    $r = new WP_Query( $events_args );

    if ( $r->have_posts() ) :

What I need to do now is: Exclude the most recently updated 2 posts from this query, because these posts are already displayed somewhere else. I can't do it using multiple loops, because this is a widget and the other query is in another widget. Help would be much appreciated!

1
  • Please do not put the solution into the question. Write an answer.
    – fuxia
    Commented Dec 19, 2013 at 2:33

2 Answers 2

1

I think you should be able to use an offset argument.

$events_args = array(
    'offset' => 2, // or whatever number you need
    'post_type' => VA_EVENT_PTYPE,
    'posts_per_page' => $number,
    // ...

I can't tell if you need pagination. If so, it gets a bit trickier but the Codex offers workarounds and there are relevant questions on this site if you search for them.

1
  • I'm not able to use offset, because the posts are ordered in a custom way to show UPCOMIG events and i want to exclude the 2 most recently updated events. I think the best way to do it actually is to run the query the other widget is running before and just paste the post IDs into an array without displaying anything. then running the actually query and excluding the posts in the array. can someone tell me how the code for that should look like? I should use 'post__not_in' => $events_already_displayed but how can i paste post Ids in this array using a loop?
    – Philip
    Commented Dec 19, 2013 at 0:51
1

Found a working solution: Before the actual query i have a pre-query that determines the ID's of the posts already displayed somewhere else and pastes them into a variable

        // EXTRA QUERY TO DETERMINE WHICH EVENTS HAVE ALREADY BENN DISPLAYED BY THE UPDATED EVENT WIDGET
    $events_args = array(
        'post_type' => VA_EVENT_PTYPE,
        'posts_per_page' => 2,
        'no_found_rows' => true,
        'post_status' => 'publish',
        'ignore_sticky_posts' => true,
        'orderby' => 'modified',
        'order' => 'DESC',
    );
    $biglocal_events_already_displayed = array();       
    $r = new WP_Query( $events_args );
    if ( $r->have_posts() ) :

    while ( $r->have_posts() ) : $r->the_post();
    $biglocal_events_already_displayed[] = get_the_ID();
    endwhile; 
        // Reset the global $the_post as this query will have stomped on it
        wp_reset_postdata();

    endif;
    // END EXTRA QUERY

In the actual query i exclude these posts by

        $events_args = array(
        // Other stuff
        'post__not_in' => $biglocal_events_already_displayed
    );

Puh....made it.

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.