• The following query works, but I’m not sure how to modify it to accomplish what I want.

    $authorPosts = get_posts(array(

    'post_type' => 'post',

    'posts_per_page' => -1,

    'meta_query' => array(

    array(

    'key' => 'author',

    'value' => '"' . get_the_ID() . '"',

    'compare' => 'LIKE'

    )

    )

    ));

    Now I want to manually include a few posts that do not have the author field value that’s in the meta_query. How can I include them anyway? Where can I integrate the following?

    'include' => array( 9048, 6289 ),

Viewing 3 replies - 1 through 3 (of 3 total)
  • It should just be:

    $authorPosts = get_posts(
    array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    include' => array( 9048, 6289 ),
    'meta_query' => array(
    array(
    'key' => 'author',
    'value' => '"' . get_the_ID() . '"',
    'compare' => 'LIKE'
    )
    )
    )
    );

    But…

    While I haven’t looked into it, the ‘include’ definition may only include those two posts and not the others from the author. You’d have to check yourself.

    If it doesn’t work the best thing to do is two separate calls and then combine the two post arrays into one. It’s a bit more custom work, but that might be the way to get it sorted out.

    Thread Starter nitrospectide

    (@nitrospectide)

    What you present is what I initially tried, but it actually returned no posts. I thought that maybe it was because setting it up this way makes it look for the intersection of these two things: a post that is one of those two listed, AND that matches the meta query. Maybe not, but it seemed to explain why no posts were returned.

    If I go the other route, can I just merge the two arrays? Any tips on getting them interleaved as if they came out as the result of one query, rather than the ‘include’ ones just being glommed on at the beginning or end?

    Moderator bcworkz

    (@bcworkz)

    Most parameters in WP_Query get logically ANDed, it’s likely what’s happening here. You want OR logic, but the only way to get that with WP_Query is to alter the actual SQL string via the “posts_request” filter. Query string manipulation is inherently weak IMO, but it is a possible solution.

    Or you could compose your own SQL and not use WP_Query. Making two queries and merging is a reasonable solution as long as the queries aren’t taking too long to execute. But merging while maintaining sort order can become difficult if the results are paginated. If not paginated, you could combine the results, then usort() the combined array. Another way would be to keep separate results arrays and alter the output loop to appropriately slip in results from the second array during output.
    .

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.