$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'); ?>terms - How to order and count get_terms by specific post type?|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)

terms - How to order and count get_terms by specific post type?

matteradmin12PV0评论

I want to get a list of taxonomy "company" terms which have a particular meta value and which are associated with posts of custom type "viewpoint".

Beginning with getting Viewpoint posts, as per recent answer, WordPress' get_posts() supports populating the resulting array with a fields value of only post ids...

$vp_post_args = array(
  'post_type' => $post_type,
  'fields' => 'ids',
  'nopaging'  => 'true',
);
$viewpoint_posts = get_posts($vp_post_args);

Thus, $viewpoint_posts contains only a list of 'viewpoint' post IDs. This list can then be used to feed a get_terms() call that is further constrained by 'taxonomy' => 'company' and a related 'meta_query'...

// 2a. Get Companies matching Tag
$vp_org_term_args_for_shortlist = array(
    // Load Viewpoint post IDs in to get_terms():
    'object_ids' => $viewpoint_posts,
    // Only get Company terms
    'taxonomy'   => 'company',
    // Only company terms with Tags Checkbox value:
    'meta_query' => array(
         array(
            'key'       => 'tags',
            'value'     => $tag_section,
            'compare'   => 'LIKE'
         )
    ),
    // Order
    'orderby'    => 'count',
    'order'      => 'desc',
    'hide_empty' => false,
    // Number of companies to show in list
    'number'     => 11,
);
$viewpoint_org_terms_for_shortlist = get_terms($vp_org_term_args_for_shortlist);

So far, so good. This all works.

My problem is around the get_terms parameter 'orderby' => 'count'. You see, count above denotes a term's total post count of any post type. In my case, my 'company' taxonomy is in use by more than one post type, but here I only want to be dealing with posts of type 'viewpoint'.

How do I get get_terms() to only think within the context of a particular post type? After all, I have already taken the step of feeding it IDs of posts which all had the same post type.

Here is my current output part...

// List organisations:
echo '<div class="list-group list-unstyled">';
foreach ($viewpoint_org_terms_for_shortlist as $org_short_single) {
     echo '<a href="' . esc_url( get_term_link( $org_short_single ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $org_short_single->name ) ) . '" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center">';
     echo '<span><img src="='.get_field( 'domain', $org_short_single ).'" class="mr-2">' . $org_short_single->name . '</span>';
     echo '<span class="badge badge-primary badge-pill">'.$org_short_single->count.'</span>';
     echo '</a>';
}
echo '</div>';

But I want to both:

  • order my company terms list by associated number of viewpoint posts (not by posts of all types).
  • output a post count for said company term that is post type viewpoint-specific (not $org_short_single->count, which again encompasses all posts)
Post a comment

comment list (0)

  1. No comments so far