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
asget_posts
already passes a few important defaults toWP_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+)