Support

Account

Home Forums ACF PRO Query posts by one custom field, sort by another

Solved

Query posts by one custom field, sort by another

  • Apologies if this is the wrong section to post this, but, I’ve built two custom fields that I’d like to use to display curated content on my front page. The fields appear on both posts and pages. The first is a True / False field, the second is a Radio Button field (options are: one, two, three…) to establish the display order. Each field is set manually.

    I can set up the custom WP query so the posts/pages that return TRUE appear on the front page, but I can’t figure how to then set the orderby parameters to match the set radio button selection.

    This is as far as I’ve gotten, which obviously isn’t correct:

    $curated_content_query = new WP_Query(array(
    ‘post_type’ => array(‘post’, ‘page’),
    ‘posts_per_page’ => -1,
    ‘volume’ => $volume,
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘curated_content’,
    ‘value’ => ‘1’,
    ‘compare’ => ‘=’
    ),
    ),
    ‘orderby’ => ‘meta_value’,
    ‘meta_key’ => ‘curated_order’
    ));

    Is the issue the fact I’m using a radio button field (when I should be using a different field for a numerical value), or do I need to add an additional array to the meta_query?

  • What are the possible values of the radio field? I don’t see any reason why this should not be working even with a radio field, unless it’s a radio group field maybe?

  • The values and labels are:

    one : 1
    two : 2
    three : 3
    four : 4
    five : 5
    six : 6
  • well, it would be sorting by the value, not the label so the sort order woudl be

    
    five
    four
    one
    six
    three
    two
    
  • Oy you are totally correct. Because I’m dealing with numbers, I was erroneously thinking cardinally and not alphabetically. Thanks!

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

The topic ‘Query posts by one custom field, sort by another’ is closed to new replies.