简体   繁体   中英

Wordpress: How to display post count in author page by custom taxonomy

I am trying to display the custom taxonomy in author page with a counter but seems i don't know how to do it.

i have a code in function.php

    add_action( 'pre_get_posts', function ( $q ) {

    if( !is_admin() && $q->is_main_query() && $q->is_author() ) {

        $q->set( 'posts_per_page', 100 );
        $q->set( 'post_type', 'custom_feedback' );

    }

});

and in my author page :

<div class="feedback-respond"> 
         <h3 class="feedback-title">User Feedback </h3>
             <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
               <?php the_content(); ?>
             <?php endwhile; else: ?>
               <p><?php _e('No posts by this author.'); ?></p>
             <?php endif; ?> 
            </div>

The code works for all the author profile but i don't know how to get the the custom taxonomy to display like this:

User Feedback

6 POSITIVE feedback 4 NEGATIVE feedback

all feedback goes here

all feedback goes here

all feedback goes here

By the way it is a custom post type (custom_feedback)and custom taxonomy(feedback_taxonomy) with two category Positive and Negative.

Please help masters?

Your only way to acvieve this will be to run two separate queries and counting the posts returned from the two separate queries. For this we will use get_posts as get_posts already passes a few important defaults to WP_Query to make the query faster and more performance orientated.

We will add one huge time and resource saver to the query, 'fields' => 'ids' . What this does is, it fetches only the post ids, and not the complete post object. This can cut query time and db queries by 99%, so even though you are going to run 2 separate queries on the complete database, the loss in page performance will be unnoticable.

Lets put everything in code ( This goes into author.php, and note, this code is untested and needs at least PHP 5.4+ )

$author_id = get_queried_object_id(); // Gets the author id when viewing the author page

// Add our term slugs into an array. 
$terms = ['positive', 'negative']; // Just make sure these values are correct
$count = [];
foreach ( $terms as $term ) {
    // Build our query
    $args = [
        'nopaging' => true,
        'post_type' => 'custom_feedback',
        'author' => $author_id,
        'tax_query' => [
            [
                'taxonomy' => 'feedback_taxonomy',
                'field' => 'slug',
                'terms' => $term
            ],
        ],
        'fields' => 'ids'
    ];
    $q = get_posts( $args );

    // Count the amount of posts and add in array
    $count[$term] = count( $q );
}

// Display our text with post counts, just make sure your array keys correspond with your term slugs used
$positive = ( isset( $count['positive'] ) ) ? $count['positive'] : 0;
$negative =( isset( $count['negative'] ) ) ? $count['negative'] : 0;

echo $positive . ' POSITIVE feedback ' . $negative . ' NEGATIVE feedback';

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