Support

Account

Home Forums General Issues Order Post By Custom Field

Helping

Order Post By Custom Field

  • Hi,

    I’m trying to do exactely what is described in the second example of the documentation (Getting featured custom posts first)

    https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/

    So I created a Boolean checkbox to say if an article is featured or not. That works ok but only of posts where this value has been set (or has been set and the removed), and thus ignoring all posts where this field has never been checked (most of the posts).

    what is the best way to handle this situation? force that all post get this flag set? How do I handle the existing posts?

    I tried to do a double condition query but that does not result in the correct post order :

    array(
    ‘relation’ => ‘OR’,

    // Never featured
    array(
    ‘key’ => ‘Sticky’,
    ‘compare’ => ‘NOT EXISTS’
    ),

    // Featured but unpromoted
    array(
    ‘key’ => ‘Sticky’,
    ‘value’ => 1,
    ‘compare’ => ‘!=’
    )
    )

    Thanks for your help

  • The best solution I’ve found is this https://wordpress.stackexchange.com/questions/138781/order-by-optional-meta-key, see the first answer that uses a “post_results” filter.

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

You must be logged in to reply to this topic.