Skip to content

Instantly share code, notes, and snippets.

@rachelbaker
Forked from dianekinney/foodquery.php
Last active March 20, 2016 17:24
Show Gist options
  • Save rachelbaker/68751ff4383cfbc9e47b to your computer and use it in GitHub Desktop.
Save rachelbaker/68751ff4383cfbc9e47b to your computer and use it in GitHub Desktop.
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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment