Php 按与当前帖子共享的标签数量对wordpress帖子进行排序
到目前为止,我的代码就在这里。它会拉入所有与当前帖子共享标签的帖子,但我不知道如何以降序方式对它们进行排序,其中第一篇帖子与当前帖子共享的标签最多。任何想法都将不胜感激Php 按与当前帖子共享的标签数量对wordpress帖子进行排序,php,wordpress,Php,Wordpress,到目前为止,我的代码就在这里。它会拉入所有与当前帖子共享标签的帖子,但我不知道如何以降序方式对它们进行排序,其中第一篇帖子与当前帖子共享的标签最多。任何想法都将不胜感激 $tags = wp_get_post_tags( $post->ID, 'Asset' ); if ( $tags ) { $tag_ids = array(); foreach( $tags as $individual_tax ) $tag_ids[] = $individual_tax->te
$tags = wp_get_post_tags( $post->ID, 'Asset' );
if ( $tags ) {
$tag_ids = array();
foreach( $tags as $individual_tax ) $tag_ids[] = $individual_tax->term_id;
$args = array(
'tag__in' => $tag_ids,
'post_type' => 'Asset',
'post__not_in' => array( $post->ID ),
'posts_per_page' => 3,
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
while ( $my_query->have_posts() ){
$my_query->the_post();
get_template_part( 'asset' );
}
}
wp_reset_query();
}
更新代码:
$tags = wp_get_post_tags( $post->ID, 'Asset' );
if ( $tags ) {
$tag_ids = array();
foreach( $tags as $individual_tax ) $tag_ids[] = $individual_tax->term_id;
$args = array(
'tag__in' => $tag_ids,
'post_type' => 'Asset',
'post__not_in' => array( $post->ID ),
'posts_per_page' => 3,
);
function order_results($post1, $post2) {
$tags1 = wp_get_post_tags($post1);
$tags2 = wp_get_post_tags($post1);
$tag_cnt1 = 0;
$tag_cnt2 = 0;
foreach ($tags1 as $tag) {
if (in_array($tag->term_id, $tag_ids)) {
}
}
foreach ($tags2 as $tag) {
if (in_array($tag->term_id, $tag_ids)) {
++$tag_cnt2;
}
}
return ($tag_cnt1 < $tag_cnt2) ? -1 : 1;
}
$my_query = new wp_query( $args );
$posts = $my_query->posts;
// Sort
usort( $posts, 'order_results');
// Print
foreach ($posts as $post) {
get_template_part( 'asset' );
}
wp_reset_query();
}
$tags=wp\u get\u post\u tags($post->ID,'Asset');
如果($tags){
$tag_id=array();
foreach($tags as$individual\u tax)$tag\u id[]=$individual\u tax->term\u id;
$args=数组(
'tag\u in'=>tag\u id,
“post_类型”=>“资产”,
'post\u not\u in'=>数组($post->ID),
“每页帖子数”=>3,
);
函数顺序\u结果($post1,$post2){
$tags1=wp\u get\u post\u标签($post1);
$tags2=wp\u get\u post\u标签($post1);
$tag_cnt1=0;
$tag_cnt2=0;
foreach($tags1作为$tag){
if(在数组中($tag->term\u id,$tag\u id)){
}
}
foreach($tags2作为$tag){
if(在数组中($tag->term\u id,$tag\u id)){
++$tag_cnt2;
}
}
返回($tag_cnt1<$tag_cnt2)?-1:1;
}
$my\u query=新的wp\u查询($args);
$posts=$my_query->posts;
//分类
usort($posts,'order_results');
//印刷品
foreach(员额为$员额){
获取模板零件(“资产”);
}
wp_reset_query();
}
最终工作代码:
$tags = wp_get_post_tags( $post->ID, 'Asset' );
if ( $tags ) {
$tag_ids = array();
foreach( $tags as $individual_tax ) $tag_ids[] = $individual_tax->term_id;
$args = array(
'tag__in' => $tag_ids,
'post_type' => 'Asset',
'post__not_in' => array( $post->ID ),
'posts_per_page' => 3,
);
// Sort
function order_results($post1, $post2) {
//Get current post tags
global $tag_ids;
// Reduce down to tags ids
$tags1 = wp_get_post_tags($post1->ID);
$tags2 = wp_get_post_tags($post2->ID);
$tags_num_1 = array();
$tags_num_2 = array();
foreach( $tags1 as $tags ) $tags_num_1[] = $tags->term_id;
foreach( $tags2 as $tags ) $tags_num_2[] = $tags->term_id;
// Set tag count
$tag_cnt1 = 0;
$tag_cnt2 = 0;
// Count tags
foreach ($tags_num_1 as $tag) {
if (in_array($tag, $tag_ids)) {
$tag_cnt1++;
}
}
foreach ($tags_num_2 as $tag) {
if (in_array($tag, $tag_ids)) {
$tag_cnt2++;
}
}
// Order
return ($tag_cnt1 < $tag_cnt2) ? 1 : -1;
}
$my_query = new wp_query( $args );
$posts = $my_query->posts;
usort( $posts, 'order_results');
// Print
foreach ($posts as $post) {
setup_postdata( $post );
get_template_part( 'asset' );
}
wp_reset_query();
$tags=wp\u get\u post\u tags($post->ID,'Asset');
如果($tags){
$tag_id=array();
foreach($tags as$individual\u tax)$tag\u id[]=$individual\u tax->term\u id;
$args=数组(
'tag\u in'=>tag\u id,
“post_类型”=>“资产”,
'post\u not\u in'=>数组($post->ID),
“每页帖子数”=>3,
);
//分类
函数顺序\u结果($post1,$post2){
//获取当前帖子标签
全球$tag_id;
//减少到标签ID
$tags1=wp\u get\u post\u标记($post1->ID);
$tags2=wp\u get\u post\u标签($post2->ID);
$tags_num_1=array();
$tags_num_2=array();
foreach($tags1作为$tags)$tags\u num\u 1[]=$tags->term\u id;
foreach($tags2作为$tags)$tags\u num\u 2[]=$tags->term\u id;
//设置标记计数
$tag_cnt1=0;
$tag_cnt2=0;
//计数标签
foreach($tags\u num\u 1作为$tag){
if(在数组中($tag,$tag_id)){
$tag_cnt1++;
}
}
foreach($tags\u num\u 2作为$tag){
if(在数组中($tag,$tag_id)){
$tag_cnt2++;
}
}
//命令
返回($tag\u cnt1<$tag\u cnt2)?1:-1;
}
$my\u query=新的wp\u查询($args);
$posts=$my_query->posts;
usort($posts,'order_results');
//印刷品
foreach(员额为$员额){
设置_postdata($post);
获取模板零件(“资产”);
}
wp_reset_query();
}WP\u查询不允许您指定任意排序条件。之后需要对结果进行排序,例如
function order_results($post1, $post2) {
$tags1 = wp_get_post_tags($post1->ID);
$tags2 = wp_get_post_tags($post2->ID);
$tag_cnt1 = 0;
$tag_cnt2 = 0;
foreach ($tags1 as $tag) {
if (in_array($tag->term_id, $tag_ids)) {
}
}
foreach ($tags2 as $tag) {
if (in_array($tag->term_id, $tag_ids)) {
++$tag_cnt2;
}
}
return ($tag_cnt1 < $tag_cnt2) ? -1 : 1;
}
usort( $my_query->posts, 'order_results');
函数顺序\u结果($post1,$post2){
$tags1=wp\u get\u post\u标记($post1->ID);
$tags2=wp\u get\u post\u标签($post2->ID);
$tag_cnt1=0;
$tag_cnt2=0;
foreach($tags1作为$tag){
if(在数组中($tag->term\u id,$tag\u id)){
}
}
foreach($tags2作为$tag){
if(在数组中($tag->term\u id,$tag\u id)){
++$tag_cnt2;
}
}
返回($tag_cnt1<$tag_cnt2)?-1:1;
}
usort($my_query->posts,'order_results');
谢谢!我理解这里发生了什么,但我不确定如何将其转化为实际影响输出。有什么帮助吗?WP have_posts()和_post()只是普通PHP的包装。我只需提取$posts=$my_query->posts这样的帖子,对它们进行排序,然后使用普通的PHP循环,比如foreach($posts as$post),而不是WP循环。这就设置了像_title()之类的东西。当你说它不起作用时,实际会发生什么?它返回帖子,但它们不是按常用标记排序的。它们按id.Oops降序排列。我刚注意到我的答案有错。将参数更改为wp_get_post_标记为ID。请参阅修改后的答案。