Skip to content

Instantly share code, notes, and snippets.

@elvismdev
Created October 19, 2017 04:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elvismdev/61f8509eff8abcc21ef84154b74fbf56 to your computer and use it in GitHub Desktop.
Save elvismdev/61f8509eff8abcc21ef84154b74fbf56 to your computer and use it in GitHub Desktop.
Combine tax_query and meta_query in WP_Query()
<?php
/**
* This is a kind of rare combination of arguments for WP_Query(), needed in some very special cases.
* This is an already tested snippet code that works!
*/
// Bring post from the global context (if not present already).
global $post;
// Define the post_type's to query for.
$post_types = array( 'event', 'post', 'book' );
// Do the weird query. Play with, or add arguments as needed https://codex.wordpress.org/Class_Reference/WP_Query
$results = WP_Query(
array(
'post_type' => $post_types,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => wp_get_post_categories( $post->ID )
)
),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'presenters_people',
'value' => $post->ID,
'compare' => 'LIKE'
),
array(
'key' => 'author',
'value' => $post->ID,
'compare' => 'LIKE'
)
)
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment