Showcase the most viewed custom taxonomies from the WordPress post cycle and remove duplicate values
P粉547362845
P粉547362845 2024-02-26 15:00:25
0
1
400

I'm creating a loop that displays the most visited taxonomies on the website. I know WordPress doesn't track taxonomies and category views. So I inserted a tracker into the posts to create a loop with the most viewed posts and then display that post's taxonomy on the home page.

Code credit to isitwp

function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
 
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

After inserting the tracker, create a loop to display the most viewed posts

code show as below:

<?php
$popular = array(
    'post_type'         => 'videos',
    'posts_per_page'    => 8,
    'meta_key'          => 'post_views_count', // setPostViews($postID) function;
    'orderby'           => 'meta_value_num',
    'order'             => 'ASC',
    'offset'            => 1,
);

$popular_loop = new WP_Query( $popular );

if( $popular_loop->have_posts() ){

    while( $popular_loop->have_posts() ) : $popular_loop->the_post();

        $terms = get_the_terms( $post->ID, 'seasons' ); 

        foreach($terms as $term) {

            $ids = $term->term_id;

            $arr = explode( ',', $ids );

            $arr_unique = array_unique( $arr );

            $str = implode( $arr_unique );

            if($term->parent != 0){
                /**
                 * Taxonomie has children get the parent ID
                */
                echo '<p>Return parent unique:' . $str . '</p>';
            
            } else {
                /**
                 * Taxonomie has NO children get the current taxonomy ID 
                */
                echo '<p>Return unique:' . $str . '</p>';
            }

        }

    endwhile;

} else {
    return false;
} wp_reset_postdata();

The problem is, because I want it to show name,id,link,image etc.. The most viewed posts have started repeating themselves in their parent taxonomy and I want to exclude the duplicates so that they are not repeated every time someone visits a post with a different taxonomy. I tried array_unique() But it keeps returning me with duplicate values.

Is there any way to remove duplicate values ​​within a loop?

P粉547362845
P粉547362845

reply all(1)
P粉545910687

I managed to solve my problem, I apologize if my question was confusing or didn't satisfy some users

code show as below:

 'videos',
    'posts_per_page' => 8,
    'order' => 'ASC',
);

$popular_loop = new WP_Query( $popular );

if( $popular_loop->have_posts() ){

    // Get the terms array
    $unique = [];

    while( $popular_loop->have_posts() ) : $popular_loop->the_post();

        // Taxonomy loop post
        $terms = get_the_terms( $post->ID, 'seasons' );

        foreach($terms as $term) {

            // Get the unique array from loop
            if( !in_array( $term->term_id, $unique ) ){
                $unique[] = $term->term_id;

                // Get unique ID from Taxonomy
                echo $term->term_id;

                // Get Unique name fromt Taxonomy
                echo $term->name;
                
            }

        }

    endwhile;

} else {

    echo 'Nothing';

} wp_reset_postdata();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template