Community Forums › Forums › Archived Forums › General Discussion › Custom Taxonomy Query
- This topic has 1 reply, 1 voice, and was last updated 7 years, 4 months ago by yvonne.
-
AuthorPosts
-
December 21, 2016 at 4:26 pm #197966yvonneMember
I have a custom post type (Media Articles) which uses some custom fields and has some custom taxonomies. I have no problem properly outputting all custom posts using a CPT archive template.
However, I also have a menu in the sidebar of the various custom taxonomies I have registered for this CPT for the purpose of filtering the custom posts by custom taxonomy. I have a custom taxonomy template set up with a query. I am able to successfully display the taxonomies either in combination or on their own depending on what taxonomy term is placed in the args. So it's working as expected - IF I didn't need to use the filtering menu. But when I try to use the menu of taxonomies, every menu item displays the same thing, which is whatever taxonomies are listed in the query args.
What I need to do is have EACH custom taxonomy in that menu display ONLY that taxonomy's CPTs. Yes, I could make one template for each custom taxonomy (all 14 of them!), but where's the fun in that? Also, the client will likely be making more taxonomies on the fly so it would need to accommodate newly generated ones.
In essence, I would like to have one query/template for all custom taxonomies that would dynamically display whichever taxonomy term is clicked.
Can anyone help me push this to the next level?
This is my current code:
$date = get_field('pub_date', false, false); $date = new DateTime($date); $args = array( 'post_type' => 'media_articles', 'post_status' => 'publish', 'posts_per_page' => '10', 'meta_key' => 'media_publication_date', 'orderby' => 'meta_value', 'order' => 'DESC', // This is the section I need to be dynamic, not hard coded into the query 'tax_query' => array( 'relation' => 'OR', array( 'taxonomy' => 'media-article-category', 'field' => 'slug', 'terms' => array( 'focus-on-the-lands' ), // ), array( 'taxonomy' => 'media-article-category', 'field' => 'slug', 'terms' => array( 'the-local-story' ), // ), ), ); $query = new WP_Query( $args ); if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <div class="media-item"> <h2><a href="<?php the_field('media_url'); ?>" target="_blank"><?php the_title(); ?><i class="fa fa-external-link" aria-hidden="true"></i></a></h2> <?php echo '<p class="lol-media-date">Publication Date: ' . DateTime::createFromFormat('Ymd', get_field('media_publication_date'))->format('F j, Y') . '</p>'; echo '<p>' . get_the_term_list( $post->ID, 'media-article-topics', 'Tags: ', ', ' ) . '</p>'; echo '<p class="lol-media-description">' . get_field('media_description') . '<span class="lol-media-publication">' . get_field('media_publication') . '</span></p>'; ?> </div><!-- end .media-item --> <?php endwhile; endif;
December 21, 2016 at 8:23 pm #197971yvonneMemberI just realized I've used some incorrect terminology but I can't find a way to edit my original post above.
I'm actually looking to filter by the custom taxonomy's TERMS, not by a bunch of custom taxonomies. So to recap:
- I have ONE custom post type
- I have ONE custom taxonomy (Media Article Category)
- Media Article Category has several terms (14 I think), 3 parent terms and the rest are child terms.
- the sidebar custom menu used for the filtering is listing the TERMSI want to filter all the CPTs for each term so that the posts for just that one term that is clicked show up.
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.