get_terms() but with additional dimensions?

I think I have cracked this, thanks to a Redditor’s help.
The advice was, effectively:

  • start with 3 and 4 (“Use get_posts() to build an array of post ids that match conditions 3 and 4”)
  • then refine by 1 and 2 (“then drop that array into the object_ids argument for your get_terms() query”)…

Like…

// 1. First, get all (4) Viewpoint (taxonomy term) (3) Articles (post type).
$vp_post_args = array(
  'post_type' => 'article',
  'tax_query' => array(
        array(
            'taxonomy' => 'format',
            'field' => 'slug',
            'terms' => 'viewpoint'
        )
    ),
    'fields' => 'ids',
  'nopaging'  => 'true',
);

// 2b. [Longlist for WP_Query] Then constrain by (1) Company (taxonomy) and its (2) Tag (ACF Checkbox) value.
    $vp_org_term_args_for_posts = array(
        'object_ids' => $viewpoint_posts_ints,
        'taxonomy'   => 'company',
        'meta_query' => array(
             array(
                'key'       => 'tags',
                'value'     => $tag_section,
                'compare'   => 'LIKE'
             )
        ),
        'hide_empty' => false,
    );
    $viewpoint_org_terms_for_posts = get_terms($vp_org_term_args_for_posts);

Leave a Comment