dianekinney
3/20/2016 - 4:46 PM

Merge query for CPT and posts in a specific category

Merge query for CPT and posts in a specific category

// Loop recipes and cooking posts
	$recipe_args = array(
		'post_type' => array( 'recipe' ),
		'post_status'     => 'publish',
		'posts_per_page' => 4,
		'fields' => 'ids', // Only fetch the post_ids here.
	);

	// Run the recipe query.
	$recipe_query = new WP_Query( $recipe_args);
	
	$cooking_args = array(
		'post_type' => 'post',
		'posts_per_page' => 4,
		'fields' => 'ids', // Only fetch the post_ids here.
		'tax_query' => array(
			array(
			'taxonomy' => 'category',
			'field' => 'slug',
			'terms' => 'cooking',
			),
		),
	);

	// Run the cooking query.
	$cooking_query = new WP_Query( $cooking_args);
	$merged_ids = array_merge( $recipe_query->posts, $cooking_query->posts );
	
	// Should be able to do something like this.
	$merge_args = array(
		'post_type' => array( 'post', 'recipe' ),
		'post__in' => $merged_ids,
		'posts_per_page' => 4, // Only return 4.
		'orderby' => 'date', // Ordered by post_date.
	);
	$querymerge = new WP_Query( $merge_args );