0
votes

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.

2
'orderby' => 'meta_value' - Mihai
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 - random_user_name
Ouch! Thank you so much, Mihai for solving the problem and cale_b for clarification! Problem solved. - george
Unfortunately, that didn't change anything, see edit above. - george

2 Answers

1
votes

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

'orderby' => 'meta_value'
0
votes

@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() ) : $bpb_query->the_post();
    //show post
  endwhile;
endif;