2

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="https://www.google.com/s2/favicons?domain='.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)
1
  • Is the get_terms_orderby filter useful here? If so, I'm not sure of the particular formulation, or whether the filter would commit me to using that filter formulation every time. Jan 11, 2019 at 16:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.