简体   繁体   中英

Retrieving a list of tags in wordpress that ALSO have another tag and/or (custom) taxonomy

I'm hoping someone might be able to point me in the right direction for this lil brain-teaser?

Basically I'm working on a wordpress site which runs on both PHP and WP-graphQL. Essentially it's a video site. The initial load is done by normal wordpress PHP and then navigating through videos is done via graphql response. I should also mention I'm using the WPGraphQL Tax Query plugin and using custom post types and taxonomies

I'm wanting to build a filtering system for it and can achieve pretty much everything I need without issue. The filter (select2) loads ALL the tags in the initial PHP load and sends the Graphql query as expected. However, there are 100's of tags so lets say the first filter request brings back 2 results and out of all those tags there are only 5 attached to these two results, I want to try and get it so that only these 5 are showing to enable a second round of filtering instead of 100's that will just just keep bringing back 'no result'. Obviously that's a basic example...

Essentially I'm wanting to filter the available tag list AS WELL AS the results themselves but I can't figure out how I might do this. I think WPGraphql might be a bit limited for this but thinking maybe if I can do it via PHP then I can make a second AJAX call and hide non-relevant results with Javascript. Obviously if anyone can give me advice on doing it in the WP-GraphQL request that would be even better....not holding out much hope on this though

I feel this sort of 'dynamic filtering' must be pretty common but I can find anything, anywhere which can point me in the right direction.

Does anyone have any thoughts/ideas that might help me achieve this?

Thanks in advance for your time: :)

Actually I believe I have figured it out so will post here in case someone else has the same question/issue.

Step 1 - Get relevant posts

$relevantItems = new WP_Query(
    array(
      'post_type' => 'video',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'fields' => 'ids',
      'tax_query' => $taxQuery
    )
  );

$taxQuery being a prebuilt tax_query object & note the 'fields' parameter resulting in just an array of ID's

Step 2 - Get relevant terms using those object ID's

$relevantTags => get_terms([
      'taxonomy' => 'post_tag',
      'fields' => 'id=>name',
      'object_ids' => $relevantItems->posts,
    ])

End result will be only terms from posts that match the tax query. In my scenario I only need the ID & name to hide non-relevant terms from my select element but omitting the 'fields' will get you the full term object.

There may be better ways to do it (eg $wpdb->prepare) but the above does indeed do the job

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM