$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'); ?>custom post types - wp_get_object_terms(): count relative to passed IDs?|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)

custom post types - wp_get_object_terms(): count relative to passed IDs?

matteradmin11PV0评论

I know when you wp_get_object_terms() you get a "count" member variable for each term object returned, but that "count" is what's in the $wpdb->term_taxonomy table in the database, it's not relative to the IDs you pass in as the first parameter. How do I get that number, though?

Example, something like:

$query = new WP_Query(array('post_type' => 'foo', 'numberposts' => 20));
$ids = wp_list_pluck($query->posts, 'ID');
$terms = wp_get_object_terms($ids, 'multi_post_type_tax');

So I want the count returned to be relevant to the query, meaning the count I get is only for the passed in post IDs. Each term object returned from the tax "multi_post_type_tax" may have a count of 400 because it applies to other post types that aren't in my query, but since I passed in specific IDs, I want that count to apply to that query, meaning a count > 20 wouldn't make any sense at all (and most likely for a particular term, it'd be less than 20, I can't see every post in a post type having the same term usually). It seems like that would work but the returned value from wp_get_object_terms has unique objects.

I know when you wp_get_object_terms() you get a "count" member variable for each term object returned, but that "count" is what's in the $wpdb->term_taxonomy table in the database, it's not relative to the IDs you pass in as the first parameter. How do I get that number, though?

Example, something like:

$query = new WP_Query(array('post_type' => 'foo', 'numberposts' => 20));
$ids = wp_list_pluck($query->posts, 'ID');
$terms = wp_get_object_terms($ids, 'multi_post_type_tax');

So I want the count returned to be relevant to the query, meaning the count I get is only for the passed in post IDs. Each term object returned from the tax "multi_post_type_tax" may have a count of 400 because it applies to other post types that aren't in my query, but since I passed in specific IDs, I want that count to apply to that query, meaning a count > 20 wouldn't make any sense at all (and most likely for a particular term, it'd be less than 20, I can't see every post in a post type having the same term usually). It seems like that would work but the returned value from wp_get_object_terms has unique objects.

Share Improve this question edited Dec 27, 2013 at 13:50 LOLapalooza asked Dec 26, 2013 at 22:21 LOLapaloozaLOLapalooza 2091 silver badge11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1 +50

I'm afraid there won't be a nice way to do this...

One way will be to:

  • get all the found posts,
  • for every one of them get its terms,
  • count the terms.

But it won't be very efficient and I wouldn't recommend this way of doing it.

On the other hand there is an efficient, but not very nice way of doing it - with SQL query:

$query = new WP_Query(array('post_type' => 'foo', 'numberposts' => 20));
$ids = wp_list_pluck($query->posts, 'ID');

$placeholders = array_fill(0, count($ids), '%d');
$format = implode(', ', $placeholders);

$results = $wpdb->get_results( $wpdb->prepare(
    " SELECT terms.name, COUNT(tr.object_id) as count FROM {$wpdb->terms} terms " .
    " INNER JOIN {$wpdb->term_taxonomy} tt ON (tt.term_id = terms.term_id) " .
    " INNER JOIN {$wpdb->term_relationships} tr ON (tr.term_taxonomy_id = tt.term_taxonomy_id) " .
    " WHERE tr.object_id IN ({$format}) GROUP BY terms.term_id ",
    $ids
) );
Post a comment

comment list (0)

  1. No comments so far