1

I'm trying to sort out a WordPress query for a Custom Post Type, but I can't make it work.

The post type is events. An Advanced Custom Fields field called sticky (yes/no) is used for sorting (stickies on top), and another ACF field called glamrock (yes/no) is used to exclude certain posts.

The result is, glamrock gets excluded, but stickies are not sorted.

If I delete the meta_query, sorting works fine, but glamrock posts are included.

$bpb_args = array(
  'numberposts'     => -1,
  'post_type'       => 'events',
  'posts_per_page'  => 100,
  'paged'           => get_query_var( 'paged', true ),
  'meta-key'        => 'sticky',
  'meta_query'      => array(
    array(
      'key'     => 'glamrock',
      'value'   => 'no',
      'compare' => 'IN',
    )
  ),
  'orderby'   => 'meta_value',
  'order'     => 'ASC',
);

$bpb_query = new WP_Query( $bpb_args );

if( $bpb_query->have_posts() ):
  while( $bpb_query->have_posts() ) : $bpb_query->the_post();
  //show post
  endwhile;
endif;

Update:

Unfortunately, meta_value instead of meta_value_num didn't change anything. It still seems to be sorting by date/time.

The Advanced Custom Field type is Radio Buttons.

In addition to the arguments, I also included the loop.

5
  • 2
    'orderby' => 'meta_value' Commented Jan 3, 2016 at 16:13
  • 1
    your orderby specifics a numeric comparison, where it would seem it's a 'yes' or 'no' value (using your meta query as a point of reference). @Mihai has it right - change your orderby to simply meta_value Commented Jan 3, 2016 at 16:25
  • Ouch! Thank you so much, Mihai for solving the problem and cale_b for clarification! Problem solved. Commented Jan 3, 2016 at 16:31
  • Unfortunately, that didn't change anything, see edit above. Commented Jan 4, 2016 at 13:08
  • this may be really old but may still help people newly encountering it... "meta-key" should be meta_key Commented Feb 10, 2023 at 13:41

2 Answers 2

3

You need to specify only meta_value since your meta-key is non numeric

'orderby' => 'meta_value'
Sign up to request clarification or add additional context in comments.

Comments

2

@Mihai had it right: meta_value_num was the main issue. I changed it to meta_value.

Here's the query/loop that worked:

$args = array(
  'post_type'       => 'events',
  'post_status'     => 'publish',
  'posts_per_page'  => -1,
  'meta_key'        => 'sticky',
  'orderby'         => 'meta_value',
  'order'           => 'ASC',
  'meta_query'      => array(
    array(
      'key'         => 'lizenz_erteilt',
      'value'       => 'no',
      'compare'     => 'LIKE',
    ),
  )
);

$query = new WP_Query( $args );

// Loop
if( $query->have_posts() ) :
  while( $query->have_posts() ) : $query->the_post();
    //show post
  endwhile;
endif;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.