I wonder if anyone can help me with this little complex query with ACF.

I have the following so far which should display:

  • Pages,

  • News Articles,

  • About Articles and

  • Involved Articles.

  • Show: only 4.

Only display if: the ACF/Meta Key ‘home_page’ is selected ‘yes’


Order by the numeric value of the ACF/Meta Key ‘home_order’ i/e 1,2,3 first, second, third etc

      $args = array(

                'post_type' => array( 'page', 'news-articles', 'about-articles’,'involved-articles' ),
                'showposts' => 4,
                'meta_query' => array(
                         'relation' => 'AND',
                                                 'key' => 'home_page',
                                                 'value' => 'yes',
                                                 'compare' => 'IN',

                                                 'orderby'   => 'meta_value_num',
                                                 'meta_key'  => 'home_order’,



I think something is going wrong with the ‘compare’ and ‘relation'

2 Answers 2


You should really read the documentation on meta_query's

Here is what is allowed in a meta_query

  • key (string) - Custom field key.

  • value (string|array) - Custom field value. It can be an array only when compare is 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN'. You don't have to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up.

    (Note: Due to bug #23268, value is required for NOT EXISTS comparisons to work correctly prior to 3.9. You must supply some string for the value parameter. An empty string or NULL will NOT work. However, any other string will do the trick and will NOT show up in your SQL when using NOT EXISTS. Need inspiration? How about 'bug #23268'.)

  • compare (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' and 'NOT EXISTS'. Default value is '='.

  • type (string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.

Your ordering needs to take place outside your meta_query. You can try the following

$args = [
    'post_type'      => ['page', 'news-articles', 'about-articles', 'involved-articles'],
    'posts_per_page' => 4,
    'meta_key'       => 'home_order',
    'orderby'        => 'meta_value_num',
    'meta_query'     => [
            'key'      => 'home_page',
            'value'    => 'yes',

Additional notes:

  • As from PHP 5.4 you can use short array syntax, ie, you can use [] instead of array(). If you still running any version before PHP 5.6, you should seriously consider upgrading.

  • showposts is dropped in favor of posts_per_page, so rather use posts_per_page

  • 1
    @birgire thanks, yes, it was a quick copy and paste error ;-) Feb 5, 2016 at 6:46
  • Yes i read through all those but couldn’t understand it all fully to get it in the right order. I figured it out with some help from else where with the following..
    – user4630
    Feb 5, 2016 at 9:52
$args = array(
                'posts_per_page'    => 4,
                'post_type' => array( 'page', 'news-articles', 'about-gorilla','involved' ),
                'orderby'   => 'meta_value_num',
                'meta_key'  => 'home_order',
                'order'     => 'ASC',
                'meta_query'    => array(
                        'key'       => 'home_page',
                        'value'     => 'yes',
                        'compare'   => '=',
  • 1
    Please don't dump code as an answer, add some explanations to it, thanks. But this looks almost the same as the answer by @PieterGoosen, apart from the ordering. Note that compare is = by default
    – birgire
    Feb 5, 2016 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.