0

I have a review section on the WP website.

I'd like to count posts with the same titles inside the 'review' category and display the counter.

I managed to do it with the tag field:

                <?php
                global $post;
                $postslist = get_posts( array( 'post_type' => 'reviews', 'posts_per_page' => 20, 'order'=> 'ASC', 'orderby' => 'date' ) );
                foreach ( $postslist as $post ){
                    setup_postdata($post);
                    ?>

                            <?php

                                // Tag counter 
                                $post_tags = get_the_tags($post->ID);
                                if ($post_tags) {       
                                    foreach($post_tags as $tag){
                                        echo '<span class="review-count">' . $tag->count . ' review(s)'  . '</span>';
                                }
                            }
                            ?>

But thought it would be more convenient to use title field instead

1

I'm assuming your original code has a closing } at the end, like this:

<?php
    global $post;
    $postslist = get_posts( array( 'post_type' => 'reviews', 'posts_per_page' => 20, 'order'=> 'ASC', 'orderby' => 'date' ) );
    foreach ( $postslist as $post ){
        setup_postdata($post);

        // Tag counter 
        $post_tags = get_the_tags($post->ID);
        if ($post_tags) {       
            foreach($post_tags as $tag){
                echo '<span class="review-count">' . $tag->count . ' review(s)'  . '</span>';
            }
        }
    }
?>

You'll want to set up an array to hold all the post titles before your foreach loop so it persists.

<?php
    global $post;
    $postslist = get_posts( array( 'post_type' => 'reviews', 'posts_per_page' => 20, 'order'=> 'ASC', 'orderby' => 'date' ) );
    // Set up an empty array
    $review_titles = array();
    foreach ( $postslist as $post ) {
        setup_postdata($post);

        // Use the title rather than tags
        $review_title = $post->post_title;
        // See if this title already exists in the array
        foreach($review_titles as $key => $val) {
            // If the title matches, add to its count
            if($val['title'] == $review_title) {
                $count = $val['count'] + 1;
                $review_titles[$key]['count'] = $count;
                // Stop looping since we already found a match
                break;
            }
            // The title doesn't match, so add it to the array with count 1
            else {
                $review_titles[] = array('title' => "$review_title", 'count' => 1);
            }
        }
    }
?>
1
  • Could you also tell me how I could echo the $count if it matches?
    – Rookie
    Mar 19 '18 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.