Support

Account

Home Forums Front-end Issues WP_Query based on one custom field and order by another

Solved

WP_Query based on one custom field and order by another

  • I have found these two resources:

    http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/

    and

    http://www.advancedcustomfields.com/resources/how-to/orde-posts-by-custom-fields/

    which individually address what I want to do, just not too sure how to integrate them together – as in query a CPT by a custom field and display in ascending order of another one (the second being a number field).

    Using both the above examples together would mean setting the ‘meta_key’ part of the query args twice which won’t work.

    Any help would be great, cheers

  • Solved it. For reference, build the query as if your were querying multiple custom fields. This frees up the ‘meta_key’ option for use in ordering.

    Example:

    'post_type' => 'post',
    'showposts' => -1,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'acf_field',
            'value' => 'acf_field_value',
            'compare' => '='
        )
    ),
    'meta_key' => 'acf_number_field',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.