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

1

6

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?

Skricki answered 28/6, 2015 at 8:52 Comment(7)
codex.wordpress.org/Function_Reference/get_post_customUnesco
but it is not custom fields it is custom taxonomy in author.php displayed.Skricki
Let me Google that for you: wordpress.stackexchange.com/questions/10175/…Unesco
Just to be clear and in order to answer your question :-), one post can only have one term assigned to it, either positive or negative. If you have 10 posts, you need to know how many posts belongs to negative and how many belongs to positive.Misdo
@Unesco your comments makes no sense at all. You are on a wild goose case. This is really easy, and I will properly answer this later based on the OP feedbackMisdo
@PieterGoosen All he's looking for are functions to get custom post types and custom taxonomies, and I provided him with just that. All he needs to do is use the functions so he can print those values.Unesco
@PieterGoosen for example have 10 post and 6 is positive and 4 is negative how may i do it?Skricki
M
1

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';
Misdo answered 28/6, 2015 at 11:45 Comment(2)
works perfect. thanks you so much @PieterGoosen. Can i ask one more thing? is there a way to modify the the_content()? I would like to add a css class to negative and positive taxonomy.Skricki
My pleasure, glad it works. Yes, that can be done. You should start a new question for this though :-)Misdo

© 2022 - 2024 — McMap. All rights reserved.