-
-
Save rachelbaker/68751ff4383cfbc9e47b to your computer and use it in GitHub Desktop.
Merge query for CPT and posts in a specific category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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