Last active
August 18, 2018 15:34
-
-
Save nszumowski/856a0f9d2ff31cc467e99bce6dda2a93 to your computer and use it in GitHub Desktop.
Merge Multiple WP_Query Based on Post ID
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
| <?php | |
| //setup your args with extra parameter fields => ids | |
| $cat_query_args = array( | |
| 'category__in' => 123, | |
| 'fields' => 'ids', | |
| ); | |
| $meta_query_args = array( | |
| 'meta_key' => 'featured_post', | |
| 'meta_value' => 'yes', | |
| 'fields' => 'ids', | |
| ); | |
| //setup your queries args | |
| $cat_query = new WP_Query($cat_query_args); | |
| $meta_query = new WP_Query($meta_query_args); | |
| //now you got post IDs in $query->posts | |
| $allTheIDs = array_merge($cat_query->posts,$meta_query->posts); | |
| //new query, using post__in parameter | |
| $query = new WP_Query(array('posts_per_page' => 4,'post__in' => $allTheIDs)); | |
| // from https://wordpress.stackexchange.com/questions/55519/can-i-merge-2-new-wp-queryvariable-s#answer-196161 | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment