Support

Account

Home Forums General Issues Pre get Posts Show posts first if has acf value

Solved

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.

  • 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.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.