0

I need help on this, the case is I want to get TOP X tags from user post. For example user A wrote 50 post, i want to show TOP 5 tags from that user post, It 's for displaying user interest.

second important thing is i m already have the post ID's selected. $pids = array(123,345,323,456,789);

Thank you.

0

With an array containing the post IDs its easy

$post_ids = array( 167, 774, 787, 358 );
$tag_all  = array();

foreach ( $post_ids as $post_id ) {
    $tags = wp_get_post_tags( $post_id, array() );
    foreach ( $tags as $tag )
        array_push( $tag_all, $tag->name );
}

$result = array_count_values($tag_all);
arsort( $result );
$result = array_slice( $result, 0, 5 );

echo '<ol>';
foreach ( $result as $tag => $count ) {
    printf( '<li>%s (%d)</li>', $tag, $count );
}
echo '</ol>';

Walk over the post IDs, get the tags of each post. Add the tags to a result array ($tag_all). Then counting the values (array_count_values()), this gives you an array with key => value (key = tag, value= count). Now just sorting the result array (arsort()) and get the first 5 elements (array_slice()).
Create a nice outpout and finish.

  • Thank you! it's working smooth :) * just wondering/possibility using one query * – Andy May 11 '13 at 5:48

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.