Skip to content

Instantly share code, notes, and snippets.

@luancmaia
Created December 6, 2017 16:12
Show Gist options
  • Save luancmaia/2c1dbb39531ef4af9bbdd4c7d087c44a to your computer and use it in GitHub Desktop.
Save luancmaia/2c1dbb39531ef4af9bbdd4c7d087c44a to your computer and use it in GitHub Desktop.
PHP Filtro
add_action( 'pre_get_posts', 'query_filter_category' );
function query_filter_category( $query ) {
if ( $_GET && isset( $_GET['tid'] ) && $query->is_main_query() ) {
$tid = $_GET['tid'];
$tids = [];
$tax_query = ['relation' => 'OR'];
global $wpdb;
foreach($tid as $t):
$result = $wpdb->get_results( 'SELECT object_id FROM ' . $wpdb->prefix . 'term_relationships WHERE term_taxonomy_id = ' . $t );
if ( $result ) {
foreach($result as $r):
$tids[] = $r->object_id;
endforeach;
}
//$tax_query[] = [ 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $t ];
endforeach;
$query->set('post__in', $tids);
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment