I’ve looked into a lot of answers and solutions, and I’m having difficulty implementing this.
My goal: In WordPress, I want to order the posts from a custom post type (Event) to display according to a custom date field. I’ve seen other solutions for this, but they’re not working for me, as my custom key is an array in the meta value, and the solutions were not.
I can’t change the core of the code because the entire site runs on it and wasn’t created by me.
The meta key I’m using is _my_meta
, and to get post meta I use get_post_meta($post->ID,'_my_meta',TRUE);
which I will put into a variable ($meta
). All the fields I call are in an array, so $meta['img']
might be a custom image, and $meta['date']
might be a custom date.
Currently, I have everything working and pulling properly from a custom WP_Query loop. I just want the posts to pull and order themselves by a custom text field of date.
Here is the loop I am using:
<?php
$args = array(
'post_type' => 'event',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => '_my_meta',
'meta_query' => array(
array(
'key' => '_my_meta',
'value' => date('Ymd'),
'compare' => '>='
)
)
);
$new = new WP_Query( $args );
while ($new->have_posts()) : $new->the_post();
?>
So, I’m pulling from the ‘event’ post type, in ascending order, ordering by my meta value. The custom field is just a text box, in which I have put a date in the format of “20150809” (which equals date('Ymd')
).
The meta_key is _my_meta
, however I really want the information from get_post_meta($post->ID,'_my_meta',TRUE)['date']
. Is this even possible? I’m positive the method being used in functions.php isn’t ideal, but it’s what I have to work with.
I’ve tried changing meta_value
to meta_value_num
, and it moves things around but not in an ideal way. Within a month, the date seems to be in order, but the months are all out of order.
I put in some arbitrary dates, and with this current setup, it’s showing as:
November 4th
November 6th
December 5th
August 9th
Any and all help appreciated. Thank you!!