Last active
January 1, 2016 16:59
-
-
Save nielsvr/8174143 to your computer and use it in GitHub Desktop.
Display posts matching a keyword within terms of a taxonomy within WordPress
This file contains 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 | |
// Display posts matching a keyword within terms of a taxonomy | |
// Keywords: tax_query, like, terms, search, WordPress | |
$keyword = "Foo"; | |
$terms = get_terms("taxonomy", array("name__like" => esc_sql($keyword) ) ); | |
$new_terms = array(); | |
$tax_query = array(); | |
if( count($terms) > 0 ) { | |
foreach($terms AS $term) { | |
$new_terms[] = $term->slug; | |
} | |
$tax_query[] = array( | |
'taxonomy' => 'taxonomy', | |
'field' => 'slug', | |
'terms' => $new_terms | |
); | |
} | |
// get the posts | |
$posts = new WP_Query( | |
array( | |
"post_type" => "post", | |
"tax_query" => $tax_query | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment