Home › Forums › General Issues › Pre get Posts Show posts first if has acf value
I have a custom post type of jobs and I’d like to show jobs marked as featured first then display all other jobs after.
Any help would be appreciated
———————————————————
So….
Job – Featured
Job – Featured
Job – Featured
Job
Job
Job
Job
Job
Job
etc.
Hi!
I think this will help you
https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/
See Example # 2
Thanks for the advice, I’ve tried the following based on the example to no avail.
Featured is set at True / False field as mentioned in example 2.
function custom_post_type_archive_jobs($query) {
if ($query->is_post_type_archive(‘jobs’)) {
$query->set(‘orderby’, ‘meta_value’);
$query->set(‘meta_key’, ‘featured’);
$query->set(‘posts_per_page’, ‘-1’);
$query->set(‘order’, ‘ASC’);
}
}
add_action(‘pre_get_posts’, ‘custom_post_type_archive_jobs’, 9999);
After clearing my cache it appears it’s now working thanks for the help.
Hi, I’m trying to do the same thing but when I use the code above then the archive show ONLY the posts with the custom field set to true, and I need them just be the first ones, not the only ones.
Any advice?
In order to order posts by a true/false field the field must exist before any posts are created. If you create the field later, after posts have been created, then you must update every post because the posts that existed prior to the existence o a field will not have a value set in the db for that field. You cannot order posts by a value that does not exist and you will only get posts that have a value for the field set.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.