I have a different sort of post order I'm trying to return that I can't find anywhere with an answer. I'm using a wp_query to try to order posts by a list of meta values. However non of the standard orderby options do what I'm trying to achieve.
My meta values are stored as a comma list string like so:
1, 5, 10, 3, 21, 6
Then I grab those values and explode them to an array like so:
$banner_nums = get_option('banner_numbers');
$nums_array = explode( ',', $banner_nums );
I do a standard wp_query, but the catch is, I'm trying to order the display of these posts by the actual order in my $banner_nums variable, not by any normal wp_query orderby such as 'ID', 'date', 'title', 'meta_value', etc.
Display like so:
1st: post with meta_value = 1
2nd: post with meta_value = 5
3rd: post with meta_value = 10
4th: post with meta_value = 3
5th: post with meta_value = 21
6th: post with meta_value = 6
If, for example, I want to display the post with meta_value = 6 as the 2nd post I'd change my list to: 1, 6, 5, 10, 3, 21.
Here is my query that grabs my posts fine, I just can't get them to order correctly.
$slider_args = array(
'post_type' => 'property',
'posts_per_page' => -1,
'nopaging' => true
);
$my_query = array();
foreach( $nums_array as $k => $v ) {
$my_query[$k]['key'] = 'property_id';
$my_query[$k]['value'] = $v;
$my_query[$k]['compare'] = '=';
}
$slider_args['meta_query'] = $my_query;
$slider_args['meta_query']['relation'] = 'OR';
$slider_query = new WP_Query( $slider_args );
if ( $slider_query->have_posts() ) {
stuff...
}
Printing my $slider_args looks like this, which looks fine:
[post_type] => property
[posts_per_page] => -1
[nopaging] => 1
[meta_query] => Array
(
[0] => Array
(
[key] => property_id
[value] => 1
[compare] => =
)
[1] => Array
(
[key] => property_id
[value] => 5
[compare] => =
)
[2] => Array
(
[key] => property_id
[value] => 10
[compare] => =
)
[3] => Array
(
[key] => property_id
[value] => 3
[compare] => =
)
[4] => Array
(
[key] => property_id
[value] => 21
[compare] => =
)
[5] => Array
(
[key] => property_id
[value] => 6
[compare] => =
)
[relation] => OR
)
I just can't figure out how to display the posts in the correct order of
1, 5, 10, 3, 21, 6
I hope I'm making sense? Any help is much appreciated.