Error when combining with custom code to order by product attribute
-
I have added some custom code to my theme to order the shop products based on the menu order of a product attribute (drag & drop order using the Simple Custom Post Order plugin)
add_action( 'pre_get_posts', 'jwd_modify_product_query' ); function jwd_modify_product_query($query) { if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'product' ) ) { $query->set( 'orderby', 'pa_brand' ); } } // https://wordpress.stackexchange.com/a/363654/94213 // Answer by honk31 add_filter('posts_clauses', 'jwd_orderby_tax_clauses', 10, 2 ); function jwd_orderby_tax_clauses($clauses, $wp_query) { global $wpdb; $orderby = isset($wp_query->query_vars['orderby']) ? $wp_query->query_vars['orderby'] : false; if ($orderby && $orderby === 'pa_brand') { $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->terms} USING (term_id) SQL; $clauses['where'] .= " AND (taxonomy = '{$orderby}' OR taxonomy IS NULL)"; $clauses['groupby'] = "object_id"; $clauses['orderby'] = "{$wpdb->terms}.term_order ASC"; $clauses['orderby'] .= ", {$wpdb->posts}.post_name ASC"; } return $clauses; }Then I get an error
WordPress database error: [Not unique table/alias: 'MYPREFIX_term_relationships']I think the issue is that this plugin is already using the term_relationships table, so without an alias, it’s not possible to join the plugin’s code with this code.
How can I get it to work?
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘Error when combining with custom code to order by product attribute’ is closed to new replies.