I’ve subcategories(customized taxonomy).I wish to fetch all merchandise of that class in a single desk & then different class identify & it is desk.
To attain this,I grouped by & ordered by customized taxonomy.Additionally, I wish to go mother or father class (customized taxonomy) id to the question.
However once I go these 2 situations at a time , no knowledge is returned.
/* to order by taxonomy*/
perform orderby_tax_clauses( $clauses, $wp_query ) {
international $wpdb;
if ( isset( $wp_query->question['orderby'] ) && 'product_category' == $wp_query->question['orderby'] ) {
$clauses['join'] .= <<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->phrases} USING (term_id)
SQL;
$clauses['where'] .= " AND (taxonomy = 'product_category' OR taxonomy IS NULL)";
$clauses['groupby'] = "object_id";
$clauses['orderby'] = "GROUP_CONCAT({$wpdb->phrases}.identify ORDER BY identify ASC) ";
$clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
}
return $clauses;
}
Right here is my perform to fetch merchandise :
add_shortcode('product-list','get_product_list');
perform get_product_list($atts)
{
extract($atts = shortcode_atts(
array(
'cat' => '',
),
$atts
));
international $put up;
international $wp_query;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
add_filter( 'posts_clauses', 'orderby_tax_clauses', 10, 2 );
$args = array(
'post_type' => 'product',
'posts_per_page' => 50,
'orderby' => 'product_category',
'order' => 'ASC',
'paged' => $paged
);
if($atts['cat'] != "") {
$args['tax_query' ][] =array(
'taxonomy' => 'product_category',
'area' => 'id',
'phrases' => $atts['cat'],
);
}
$wp_query = new WP_Query( $args );
echo $wp_query->request;
//echo '********'.$wp_query->last_query;
remove_filter( 'posts_clauses', 'orderby_tax_clauses', 10, 2 );
if ( $wp_query->have_posts() ) :
/* my code*/
endif;
}
I debugged & discovered this question :
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT OUTER JOIN wp_term_relationships ON wp_posts.ID=wp_term_relationships.object_id
LEFT OUTER JOIN wp_term_taxonomy USING (term_taxonomy_id)
LEFT OUTER JOIN wp_terms USING (term_id)
WHERE 1=1
AND (wp_term_relationships.term_taxonomy_id IN (65))
AND wp_posts.post_type = 'product'
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'acf-disabled')
AND (taxonomy = 'product_category'
OR taxonomy IS NULL)
GROUP BY object_id
ORDER BY GROUP_CONCAT(wp_terms.identify
ORDER BY identify ASC) ASC
LIMIT 0,
50