I want to create a single array of posts by merging the results of 2 separate get_posts
queries, and have the array ordered by published date.
In my code below, the get_posts for $args_b
and $args_a
have been merged into one array, but they are separated: The 9 titles of $args_b
are listed first and then the 9 titles of $args_a
. I want them to be mixed, and ordered by date. How do I sort them?
<?php
$args_a = array(
'posts_per_page' => 9,
'category_name' => 'shinmatsudo',
'category__in' => array( 227 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '1b',
'compare' => 'NOT EXISTS'
),
array(
'key' => '1d',
'compare' => 'NOT EXISTS'
),
),
);?>
<?php
$args_b = array(
'category_name' => 'matsudo',
'posts_per_page' => 9,
'category__in' => array( 329 ),
'category__not_in' => array( 3 ),
'meta_query' => array(
'relation' => 'and',
array(
'key'=> '2a',
'value' => array('2020-02-01' , '2020-06-01'),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
),
);
?>
<?php
global $post;
$my_posts= array_merge( get_posts($args_b), get_posts($args_a) );
foreach($my_posts as $post):setup_postdata($post);?>
<?php the_title(); ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>