Filtering posts by taxonomy and meta_value
I am writing a plugin that will hide and sort posts based on tags and postmeta. Products tagged "antique" that are out of stock will be hidden by default. So far, I can hide sold products, but when I attempt to hide sold products that are tagged "antique" my code fails. I was hoping that someone could help me find a solution. I think that I am having trouble in the WHERE clause or in the LEFT JOIN statement for the terms table. Here is my code:
add_filter( 'posts_join', 'meta_join' );
function meta_join( $join ) {
if ( ( is_woocommerce() || is_search() ) ! is_admin() ) {
global $wpdb;
$add = '';
if ( ! is_product_category() ! is_product_tag() ) {
$add = " LEFT JOIN $wpdb-term_relationships ON ( $wpdb-posts.ID = $wpdb-term_relationships.object_id ) ";
}
$add .= "
LEFT JOIN $wpdb-postmeta AS stock ON (
$wpdb-posts.ID = stock.post_id
AND stock.meta_key = '_stock_status'
)
LEFT JOIN $wpdb-term_taxonomy wtt ON (
$wpdb-term_relationships.term_taxonomy_id = wtt.term_taxonomy_id
AND wtt.taxonomy = 'product_tag'
)
LEFT JOIN $wpdb-terms wt ON ( wtt.term_id = wt.term_id )
";
return $join . $add;
}
return $join;
}
add_filter('posts_where', 'hide_sold_antiques');
function hide_sold_antiques( $where ) {
if ( ! show_sold_antiques() ! is_product() ! is_search() is_woocommerce() ! is_admin() ) {
global $wpdb;
$add = " AND NOT (
stock.meta_value = 'outofstock'
AND wt.slug = 'antique'
) ";
return $where . $add;
}
return $where;
}
add_filter( 'posts_orderby', 'product_meta_orderby' );
function product_meta_orderby( $orderby ) {
if ( ( is_woocommerce() || is_search() ) ! is_admin() ) {
global $wpdb;
return " $wpdb-posts.post_type DESC, stock.meta_value ASC, $wpdb-posts.post_date DESC ";
}
return $orderby;
}
function show_sold_antiques() {
$show_sold_antiques = false;
$showsold_parameter = null;
if ( isset( $_GET[ 'showsold' ] ) ) {
$showsold_parameter = $_GET[ 'showsold' ];
if ( $showsold_parameter === 'true' ) {
$show_sold_antiques = true;
}
}
return $show_sold_antiques;
}
Topic posts-where meta-value taxonomy Wordpress
Category Web