$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom taxonomy - Query WP Posts, then list the taxonomies from those posts|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom taxonomy - Query WP Posts, then list the taxonomies from those posts

matteradmin8PV0评论

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 1
Add a comment  | 

1 Answer 1

Reset to default 0

Use 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!

Post a comment

comment list (0)

  1. No comments so far