WP Recipe Maker – query for posts with videos in recipe card

This first runs a query for recipes that have videos, then loops through those recipes and adds the “parent post id” to an array. This is the post to which the recipe was attached. We then query for those posts.

$recipes = new WP_Query( [
	'post_type' => 'wprm_recipe',
	'posts_per_page' => 8,
	'fields' => 'ids',
	'update_post_term_cache' => false,
	'ignore_sticky_posts' => true,
	'no_found_rows' => true,
	'meta_query' => [
		[
			'key' => 'wprm_video_embed',
			'value' => '',
			'compare' => '!=',
		]
	]
]);

if( empty( $recipes->posts ) )
	return;

$post_ids = [];
foreach( $recipes->posts as $recipe_id ) {
	$post_id = get_post_meta( $recipe_id, 'wprm_parent_post_id', true );
	if( !empty( $post_id ) && 'post' === get_post_type( $post_id ) )
		$post_ids[] = intval( $post_id );
}

$posts = new WP_Query( [
	'post__in'	=> $post_ids,
	'orderby'	=> 'post__in',
]);

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk