$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Order posts by taxonomy count|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Order posts by taxonomy count

matteradmin9PV0评论

I have created new post type (hotels) and taxonomies (localisation).

I want to loop my hotels ordering by count DESC of taxonomies. I want to sort my hotel by count of localisation (I mean order from more hotels in localisation to less hotels).

I try this :

    $args = array(
        'post_type' => 'hotels',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'taxonomy_localisation',
                'field'    => 'term_id',
                'orderby'    => 'count',
                'order'    => 'DESC',
                'terms'    => $localisation_child->term_id,
            ),
        ),
    );

Is it possible using WP_query args ?

I have created new post type (hotels) and taxonomies (localisation).

I want to loop my hotels ordering by count DESC of taxonomies. I want to sort my hotel by count of localisation (I mean order from more hotels in localisation to less hotels).

I try this :

    $args = array(
        'post_type' => 'hotels',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'taxonomy_localisation',
                'field'    => 'term_id',
                'orderby'    => 'count',
                'order'    => 'DESC',
                'terms'    => $localisation_child->term_id,
            ),
        ),
    );

Is it possible using WP_query args ?

Share Improve this question asked Feb 12, 2018 at 15:15 Vincent DecauxVincent Decaux 2352 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

this topic might help you. By default args of WP_Query - this is impossible.

I don't think you can do that directly. Although you can save terms count for each post in a custom meta field (on post publish) and then in your query define sort by that meta field. Or while running your query, you can save terms count along with posts ids in another array, then sort them by count and print posts. Second one will consume more resources so I would recommend first method.

SELECT p.ID,COUNT(*) count FROM wp_term_relationships r, wp_posts p, wp_term_taxonomy x WHERE r.object_id = p.ID AND p.post_type = 'post' AND r.term_taxonomy_id = x.term_taxonomy_id AND x.taxonomy = 'post_tag' GROUP BY object_id ORDER BY count;

you can't do it directly, you need a filter and a CASE in the orderby eg

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far