WordPress meta_query Not Working with ‘orderby’ – Advanced Custom Fields

I’m trying to query a custom post type ‘section’ where the ‘section.section_parent_page = {whatever the ID of the current $post is}’.

This part of the query works fine and is accomplished like this:

Read More
$args = array(
    'post_type' => 'section',
    'meta_query' => array(
        array(
            'key' => 'section_parent_page', // name of custom relate field
            'value' => '"' . get_the_ID() . '"',
            'compare' => 'LIKE'
        )
    )
);

But once I try to ‘orderby’ the custom field ‘section_position’, I either break the query or see no proper ordering. Here’s what I currently have:

$args = array(
    'post_type' => 'section',
    'meta_key' => 'section_position', // custom field I want to order by
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'section_parent_page', // name of custom relate field
            'value' => '"' . get_the_ID() . '"',
            'compare' => 'LIKE'
        )
    )
);

When I look at the $the_query->request, it looks like it continues to order by wp_posts.menu_order

How do I use multiple meta_query arrays to accomplish this?

EDIT:
No matter what I try, the end of my query string ends up looking like this:

ORDER BY wp_posts.menu_order ASC LIMIT 0, 10

Related posts

Leave a Reply

2 comments

  1. If section_position has integer ( number ) value, use following code

    $args = array('post_type' => 'section',
                  'orderby' => 'meta_value_num', 
                  'meta_key' => 'section_position',
                  'order'=>'ASC',
                  'meta_query' => array(
                                   array(
                                     'key' => 'section_parent_page', // name of custom relate field
                                     'value' => '"' . get_the_ID() . '"',
                                     'compare' => 'LIKE'
                                   )
                                  ) 
                   );
    

    Note i have used “meta_value_num” instead of “meta_value”

  2. I attempted to do the same thing recently within an ACF filter. I contacted ACF support and received this answer.

    Unfortunately i this query is too complex to be passed through WP_Query and you would have to do a manual SQL query to achieve this.
    The problem is you are doing two meta_querys, the meta_key = start date and the ones inside the brackets are conflicting.
    You need the flexibility of full SQL syntax to perform a query like that.