user1050887
user1050887

Reputation: 315

How to access an ACF field on a category from within pre_get_posts in functions.php to modify archive query

Long title, but what I'm trying to accomplish is

1) Adding a Post Object field to the Add/Edit Category page to specify a single post to use as a featured/sticky post for that category. As far as I can tell there's no way to do this (featured category post). Have used https://wordpress.org/plugins/category-sticky-post/ in the past but seems dead and starting to act buggy.

2) Display that post on top of the respective archive page 1, and then start with the normal loop for that category. This is working perfectly.

3) Where I'm stuck is how to exclude that featured post from the query so we don't get duplicates. I'm trying to do this in a pre_get_posts function in functions.php to modify the query so it functions like a normal archive page otherwise.

If you know of an easier way to simply set a featured article for a category I'm open to suggestions. I know sticky for main homepage loop but nothing for archive pages.

You may need more info but that should be a good starting point. Using ACF Pro (latest)

Thanks for the help!

Upvotes: 0

Views: 249

Answers (1)

Sagar Bahadur Tamang
Sagar Bahadur Tamang

Reputation: 2709

You can use the ignore_sticky_posts property while querying posts using WP_Query to exclude the sticky/featured posts.

Reference: WP_Query

Upvotes: 0

Related Questions