I have to fetch all the posts post_type=projects and post_status=publish which have the latest comment of type message older than 5 days with comment_meta value as key=value.

I want to know how can prepare query to execute in get_posts() function.

1 Answer
1

You can first try the following comment query:

$comments = get_comments( 
    [
        'post_type'     => 'projects',
        'post_status'   => 'publish',
        'type'          => 'message',
        'date_query'    => [
            [
                'before'    => '5 days ago midnight', 
                'inclusive' => true,
            ]
        ],
        'meta_query'    => [
            [
                'key'   => 'foo',       // <-- Adjust to your needs!
                'value' => 'bar'        // <-- Adjust to your needs!
            ]
        ]           
    ] 
);

and then collect the post ids with:

$post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );

You might want to consider to restrict the number of queried comments with the number attribute.

Then you could e.g. use it with:

$query = new WP_Query( [ 'post__in' => $post_ids, ... ] );

for a non-empty $post_ids array.

Leave a Reply

Your email address will not be published. Required fields are marked *