A WP_Query that will look for posts after 2 weeks ago OR with a certain meta value

It might be possible to achieve this by using the post clauses filters and rewriting the generated SQL query.

You can also run 3 queries,

  • One very lean query to get the desired posts from the date_query

  • One very lean query to get all the posts from the meta_query

  • One final query to get the complete query object. This help specially with pagination if you ever want to paginate the query. This also sorts the posts correctly in order.

You can try the following

$defaults = [
    'post_type'      => 'post',
    'orderby'        => 'date',
    'posts_per_page' => -1,
    'fields'         => 'ids' // Only return post ID's for performance
];

// Query 1
$date_query = [
    [
        'after' => '2 weeks ago'
    ]
];
$query1 = new WP_Query( array_merge( $date_query, $defaults ) );

// Query 2
$meta_query = [
    [
        'key'     => 'sticky',
        'value'   => true,
        'type'    => 'BOOLEAN',
    ]
];
$query2 = new WP_Query( array_merge( $meta_query, $defaults ) );

// Final query
// Get all the post ID's from the two queries and merge into one array
$ids = array_merge( $query1->posts, $query2->posts )
// Make sure we have an array of id's before continueing to avoid unexpected results
if ( $ids ) {
    // Remove possible duplicates
    $ids = array_unique( $ids );
    // Set fields back to all to get full post objects
    $defaults['fields']    = 'all';
    // Add extra parametes
    $defaults['post__in']    = $ids; // Set the array of ids to post__in
    // $defaults['order']    = 'ASC'; // If you want to keep the post order according to post__in
    //$defaults['orderby'] = 'post_in'; // If you want to keep the post order in post__in

    // Run our query
    $q = new WP_Query( $defaults );
    // You can now run your loop

Leave a Comment