Skip to content

Instantly share code, notes, and snippets.

@nielsvr
Last active January 1, 2016 16:59
Show Gist options
  • Save nielsvr/8174143 to your computer and use it in GitHub Desktop.
Save nielsvr/8174143 to your computer and use it in GitHub Desktop.
Display posts matching a keyword within terms of a taxonomy within WordPress
<?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