stuttter / wp-term-order

Sort taxonomy terms, your way
https://wordpress.org/plugins/wp-term-order/
GNU General Public License v2.0
95 stars 24 forks source link

doesn't work with WP_Query #15

Closed ben-milkshake closed 4 years ago

ben-milkshake commented 6 years ago

Hi all, thank you for the great plugin I do have custom posts and I need to make it work with custom categories this doesn't work ` $params = array( 'post_type' => 'mycustompost', 'posts_per_page' => 9999 //'orderby' => array('date' => 'ASC', 'meta_value' => 'ASC') );

    $params['tax_query'] = array(
        array(
            'taxonomy' => 'mycustomcats',
            'field' => 'term_id',
            'hide_empty'        => true,
            'terms' => $termsidparent,
            'include_children' => false,
            'orderby'    => 'order',
            'order'      => 'ASC'
        )
    );

$res = new WP_Query($params);` how can I add something that the filter works ? thank you

best

maciekpaprocki commented 6 years ago

That's not how mysql works, as your query will just include posts that are in this categories (and it doesn't care about their taxonomy order). You will have to do custom query like (pseudocode):

SELECT * FROM wp_posts
JOIN wp_term_taxonomy 
GROUP BY (p.ID)
ORDER BY wp_term_taxonomy.order

This code should give you directions on how to progress.

JJJ commented 4 years ago

@maciekpaprocki is correct. The order parameter could now live in term meta, but that would include a somewhat relatively expensive LEFT JOIN on meta_value which I'd rather avoid.