Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 927
rolex Avatar asked Jun 28 '15 08:06

rolex


People also ask

How do I get the author count in WordPress post?

To show the number of posts of the author, count_user_posts requires the ID of a user. The function must receive the integer argument representing the id of the author. Passing the user-id alone to the function counts all author posts without considering post types. The default post type the function returns is post.

How do I find the category count in WordPress?

Easy way to count category is: 1) firstly fetch all category from wordpress 2) count them using simple php funciton complete code will be like: <? php $args = array( 'parent' => 0, 'hide_empty' => 0 ); $categories = get_categories( $args ); echo "Total categories : ". count( $categories ); ?>


1 Answers

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';
like image 197
Pieter Goosen Avatar answered Oct 04 '22 02:10

Pieter Goosen