使用wordpress中的元数据返回最流行的标签

时间:2014-09-01 11:04:37

标签: php wordpress taxonomy

我正在构建复杂的标签云。

Wordpress目前有一个名为' get_tags'的内置函数。我用它来建立我网站上使用的最常用标签的列表。

这是我当前正在运行的php函数:

function top_brand_tags($related_publication) {

        $tag_data = '';
        $tags = get_tags();

        $counts = $tag_links = array();

        foreach ( (array) $tags as $tag ) 
        {
            $counts[$tag->name] = $tag->count;
            $tag_links[$tag->name] = get_tag_link( $tag->term_id );
        }

        asort($counts);
        $counts = array_reverse( $counts, true );
        $i = 0;
        foreach ( $counts as $tag => $count ) 
        {
                $i++;
                $tag_link =  str_replace(' ', '-', wp_specialchars( $tag ));
                $tag = str_replace(' ', ' ', wp_specialchars( $tag ));

                if($i < 11)
                {
                        if ($_GET['the_post_tag'] == $tag_link){$post_tag_class = "on";}else{$post_tag_class = "";}

                        $tag_data.= '<a href="'.site_url().'/see-all?the_post_tag='.$tag_link.'&related_publication='.$related_publication.'" >
                        <li class="filter-btn">'.ucfirst($tag).' ('.$count.')</li>
                        </a>';                      
                }
        }

        return $tag_data;
}

我现在需要修改此函数,以便它允许我传入元键和值。然后,该功能必须搜索具有此键和与之关联的值的所有帖子,并根据此新条件返回最常用的标签。

总结一下:我需要编写一个新函数:

1 - 搜索包含特定元键和值的所有帖子

2 - 根据这些结果返回前10个最常用的标签。

1 个答案:

答案 0 :(得分:0)

首先,获取具有所需元组的帖子并按所述元组排序。

$posts = new WP_Query( array(
    'meta_key' => 'your_meta_key',
    'orderby' => 'meta_value' // or 'meta_value_num'
    'order' => 'DESC',
    'posts_per_page' => 50 // Grabs 50 posts at maximum, which in turn gets the tags only for those posts, set to -1 to get all posts
));

然后浏览每个帖子并抓住可用的标签。

// Save each unique tag and their count in this temp array.
$tags_counts = array();

if ( $posts -> have_posts() ) : while ( $posts -> have_posts() ) : $posts -> the_post();

    $post_tags = wp_get_object_terms( get_the_ID(), 'post_tag' );

    if ( empty( $post_tags ) {
        continue; // Skip post if no tags set.
    }

    foreach ( $post_tags as $tag ) {
        if ( array_key_exists( $tag -> slug, $tags_counts ) {
            // Increment tag count if tag already present.
            $tags_counts[ $tag -> slug ] = $tags_counts[ $tag -> slug ] + 1;
        }

        else {
            // New tag found, add to the counts list.
            $tags_counts[ $tag -> slug ] = 1;
        }
    }

endwhile; endif;

// Order the tag count array from most to least. Remember to reverse the sort afterwards to get "biggest first".
asort( $tags_counts, SORT_NUMERIC ); // Use 'asort' to keep index association.
$tags_counts = array_reverse( $tags_counts, true ); // 'true' preserves keys

// If you want to limit the tags to 10 max, slice the array. Last parameter 'true' sets to preserve keys during slice operation.
$tags_counts = array_slice( $tags_counts, 0, 10, true );

现在我们已经浏览了有meta_key设置的帖子,并根据每个帖子统计了标记计数。您可以为每个人构建一个标签云:

$tag_cloud = '<ul class="custom-tag-cloud">';

foreach ( $tags_counts as $tag => $count ) { 
    $tag_obj = get_term_by( 'slug', $tag, 'post_tag' ); // get tag term object.

    // Linked element: 'tagname (count)'.
    $tag_cloud .= sprintf( '<li><a href="%s">%s (%s)</a></li>', get_tag_link( $tag_obj -> term_id ), $tag_obj -> name, $count );
}

$tag_cloud .= '</ul>';

免责声明:未经测试,但逻辑应与上述类似。