I have a unique use case that I can't seem to find an example for online. And it's possible I'm overthinking it.
I have a custom taxonomy called "grantees." When a blog post is created, the author can choose the grantee.
I have "browse by" pages so that these pages you can "Browse by grantee." This page lists all of the taxonomy of "grantees" and the number of posts associated with each grantee, using the following snippet of code inside of the WP Loop.
<ul class="list-terms">
<?php
$terms = get_terms(
array(
'taxonomy' => 'grantees',
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC'
)
);
foreach($terms as $term) {
?>
<li>
<a href="<?php $term_link = get_term_link( $term->term_id ); echo $term_link ;?>">
<?php echo $term->name ;?> (<?php echo $term->count; ?>)
</a>
</li>
<?php };?>
</ul>
This is working well. However, I need to now do this so that it pulls only a specific year (currently, 2018). I'm wanting to make archive pages based on year. My client uses this page as a way to keep track of blogs.
Is there a way to query just a specific year (say, 2018 posts), then have this page display the taxonomies associated with only the 2018 posts? I understand taxonomies don't have dates attached, but my thought is to query the 2018 posts, then ask WP to list the taxonomies of those queried posts.
I have a unique use case that I can't seem to find an example for online. And it's possible I'm overthinking it.
I have a custom taxonomy called "grantees." When a blog post is created, the author can choose the grantee.
I have "browse by" pages so that these pages you can "Browse by grantee." This page lists all of the taxonomy of "grantees" and the number of posts associated with each grantee, using the following snippet of code inside of the WP Loop.
<ul class="list-terms">
<?php
$terms = get_terms(
array(
'taxonomy' => 'grantees',
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC'
)
);
foreach($terms as $term) {
?>
<li>
<a href="<?php $term_link = get_term_link( $term->term_id ); echo $term_link ;?>">
<?php echo $term->name ;?> (<?php echo $term->count; ?>)
</a>
</li>
<?php };?>
</ul>
This is working well. However, I need to now do this so that it pulls only a specific year (currently, 2018). I'm wanting to make archive pages based on year. My client uses this page as a way to keep track of blogs.
Is there a way to query just a specific year (say, 2018 posts), then have this page display the taxonomies associated with only the 2018 posts? I understand taxonomies don't have dates attached, but my thought is to query the 2018 posts, then ask WP to list the taxonomies of those queried posts.
Share Improve this question asked Mar 4, 2019 at 22:02 aturneraturner 11 Answer
Reset to default 0Use WP_Query with date_query and within loop show only posts with 'grantees'
taxonomy using is_object_in_term()
$args = array(
'date_query' => array(
array(
'year' => 2018,
),
),
);
$the_query = new WP_Query( $args );
Then in loop
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if( is_object_in_term( $post->ID, 'grantees' )){
// show post
// and then term from taxonomy
$terms = get_the_terms( get_the_ID(), 'grantees' );
echo '</ul>';
foreach($terms as $term) {
?>
<li>
<a href="<?php $echo get_term_link( $term->term_id );?>">
<?php echo $term->name ;?> (<?php echo $term->count; ?>)
</a>
</li>
<?php
} // END foreach()
echo '</ul>';
} // END if
} // END while
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
I hope this helps.
Note: Take care of typo, if any!