帖子数量最少的前几位标签

时间:2019-05-30 作者:Alex

我在1天的时间间隔内得到了最常用的标签,一切都很好,但我如何增加限制,以获得至少5篇帖子的标签,并通过计数来排序?

Actual code:

$term_ids = $wpdb->get_col("
    SELECT term_id FROM $wpdb->term_taxonomy
    INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id=$wpdb->term_relationships.term_taxonomy_id
    INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
    WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= $wpdb->posts.post_date"            
);

$tags = get_tags(array(
    \'orderby\' => \'count\',
    \'order\'   => \'DESC\',
    \'number\'  => 10,
    \'include\' => $term_ids,
));

foreach ( (array) $tags as $tag ) {
    echo \'<li><a href="\' . get_tag_link ($tag->term_id) . \'" rel="tag">\' . $tag->name . \'</a></li>\';
}
提前感谢!

1 个回复
SO网友:Alex

I found a solution!:

$term_ids = $wpdb->get_col("
    SELECT term_id FROM $wpdb->term_taxonomy
    INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id=$wpdb->term_relationships.term_taxonomy_id
    INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
    WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= $wpdb->posts.post_date"            
);

$tags = get_tags(array(
    \'orderby\' => \'count\',
    \'order\'   => \'DESC\',
    \'number\'  => \'10\', // <-- maximum number of tags
    \'include\' => $term_ids,
    \'fields\'  => \'ids\'
));

foreach($tags as $tag) {                
    $term = get_term_by(\'id\', $tag, \'post_tag\');
    if ($term->count < 5) { // <-- minimum number of posts              
        $index = array_search($term->term_id, $tags);
        unset($tags[$index]);                   
    }               
}

foreach ( (array) $tags as $tag_result ) {
    $term = get_term_by(\'id\', $tag_result, \'post_tag\');
    echo \'<li><a href="\' . get_tag_link ($term->term_id) . \'" rel="tag">\' . $term->name . \'</a></li>\';
}