I have a custom post type 'event' that I am trying to sort by date. And for some reason it isn't working. I'm confused because I've actually already done this before without a problem. What wrong this time around?
The first three lines are to get the month, year, and state that are assigned to the page via ACF custom fields. (This is separate from the ACF fields that the events have.) Essentially I'm trying to get the events only for that state, month, and year.
<?php
$thestate = get_field('page_state');
$themonth = get_field('page_month');
$theyear = get_field('page_year');
$meta_query = array(
array(
'key' => 'event_state',
'value' => $thestate,
'compare' => '='
),
array(
'key' => 'event_month',
'value' => $themonth,
'compare' => '='
)
);
$args = array(
'meta_key' => 'date_of_event',
'order_by' => 'meta_value_num',
'order' => 'ASC',
'post_type' => 'event',
'posts_per_page' => -1,
'meta_query' => $meta_query
);
$myposts = get_posts($args);
if($myposts) { ?>
<?php foreach($myposts as $post) { ?>
<?php setup_postdata( $post ); ?>
/*--Code Here--*/
<?php wp_reset_postdata(); ?>
<?php } ?>
<?php } ?>
EDIT: I figured out the issue. It took me way too long to get this figured out but it turns out that it was caused by the fact that I was using 'order_by' whereas I needed to be using 'orderby'... Removing the underscore is all that needed to be done. I am unsure as to why this is the case though. 'order_by' seems to work for others so I'm still a little bit confused but glad I managed to fix it.