• Hi,

    my website has posts that share the exact same publish date (since they are all articles from a magazine issue), about 10-15 per issue.

    I have now run into an issue with the wordpress query results. Some posts are skipped and some posts are displayed on multiple pages of the pagination.

    Presumably, this is because when ordered by date they would share the same position and my pagination displays less posts than there are posts with the same date.

    For simple category listings, I fixed this by adding a second term (id) for the search parameter.

    However, for my search results page, this is not an option, since there posts are ordered by ‘relevance’ and it turns out you cannot add a second term if you are ordering by relevance.

    Is there a way to make this second term an option so I don’t have to change every post’s publish date?

    Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @naturundtierverlag

    The issue arises because pagination struggles to handle posts with identical dates when sorting by relevance or other single criteria. To resolve this without altering the publish dates, you can modify the query to include a unique secondary sorting criterion, such as the post ID, even when ordering by relevance. This ensures consistent pagination. In your search query, you can programmatically append post__in to the query to maintain relevance sorting and add a fallback, like orderby => ['relevance' => 'DESC', 'ID' => 'ASC'], to ensure no posts are skipped or repeated across pages. This solution avoids the need to change publish dates and maintains proper order and pagination integrity.

    Moderator threadi

    (@threadi)

    There is a whole range of hooks with which you can influence WordPress database queries. However, I’m not sure what you’re working with – are you using self-developed scripts or just ready-made plugins?

    Thread Starter naturundtierverlag

    (@naturundtierverlag)

    Hi,

    thanks for your answers.

    Weirdly enough, adding the second criterion seems to work now, even though it didn’t a few weeks ago.

    This code did the trick:

            if ($query->is_search) {
    $query->set('orderby', 'relevance ID');
    }

    Thanks again!

    Thread Starter naturundtierverlag

    (@naturundtierverlag)

    I think I’m going crazy. After a while without any changes to the search, it stopped working again. With the second criterion, it only sorts by that. Only without a second one does ‘relevance’ work.

    Example: https://www.reptilia.de/?s=schmetterlinge
    (Should show the same post on page 1 and 2 of the pagination)

    Any theories?

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Query result issue with posts of same date’ is closed to new replies.