0

I am trying to understand how I can fetch posts for an archive that:

  1. Have a unique title
  2. Are the most recent version of said unique title

So, if I have 3 posts:

title: 'ID1: My Title'
date: 01/01/18

title: 'ID1: My Title'
date: 02/01/18

title: 'ID1: My Title'
date: 03/01/18

I would only like to see the last, most recent one returned:

title: 'ID1: My Title'
date: 03/01/18

This is my current query. I am using the Events Calendar plugin, hence the post_type:

$past_events = new WP_Query(array(
    'post_type' => Tribe__Events__Main::POSTTYPE,
    'paged'     => $paged,
    'posts_per_page' => '10',
    'eventDisplay'=>'past',
    'orderby'=> 'title',
    'order' => 'ASC',
    's' => $searchText ?: '',
));

I'm wondering what to add to this query or if this is something I should try to accomplish with custom SQL? Any help would be much appreciated. Thanks!

0

You can try this. However, I am sure there must be a better alternative..

$past_events = get_posts(array(
    'post_type' => Tribe__Events__Main::POSTTYPE,
    'paged'     => $paged,
    'posts_per_page' => '10',
    'eventDisplay'=>'past',
    'orderby'=> 'title',
    'order' => 'ASC',
    's' => $searchText ?: '',
));

$post_arr = array();
foreach ( $past_events as $past_event ) {
    $post_arr[ $past_event->post_title ] = $past_event->ID;
}  

$post_arr will now contain the title of the latest post containing same title as key and the post id as value.

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.