• I currently have a query on my front page that displays the three most-recently modified posts in a certain category. I would like to have another query for the most recently added post from the same category. I often add new information to older posts and would like to keep these on the front page, but often the most recent post would appear in both queries. Is there a way to exclude the most recently published piece from the recently modified query so there isn’t two instances of this on the front page?

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’m unsure how you are querying or whether you have access to the code, but the offset property should allow you to skip the latest post.

    See:

    https://developer.www.ads-software.com/reference/classes/wp_query/#pagination-parameters

    Moderator bcworkz

    (@bcworkz)

    offset will work well to exclude most recent posts if both queries are for the same category. Otherwise it could be unreliable. If you cannot access the originating query code, you could introduce an offset query var via the “pre_get_posts” action. Your callback would need to somehow discern the correct query to alter since all queries go through this action.

    A more reliable way to exclude specific posts from another query is more involved but is more widely applicable. In the first query, as you loop through the query results, collect each post’s ID into an array. Then pass this array in the next query as the post__not_in arg.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude most recent post from query’ is closed to new replies.