Php 按标签计数的wordpress字体帖子
具有自定义的post typePhp 按标签计数的wordpress字体帖子,php,wordpress,tags,Php,Wordpress,Tags,具有自定义的post typeposttype,具有多种分类法,如tax1、tax2和post\u标签,这些分类法添加如下: function add_tags_categories() { register_taxonomy_for_object_type('post_tag', 'posttype'); } add_action('init', 'add_tags_categories'); $tags = wp_get_post_terms( $post -> ID, 'p
posttype
,具有多种分类法,如tax1
、tax2
和post\u标签
,这些分类法添加如下:
function add_tags_categories() {
register_taxonomy_for_object_type('post_tag', 'posttype');
}
add_action('init', 'add_tags_categories');
$tags = wp_get_post_terms( $post -> ID, 'post_tag', ['fields' => 'ids'] );
$args = array(
'post_type' => 'posttype',
'post__not_in' => array( $post -> ID ),
'posts_per_page' => -1,
'ignore_sticky_posts' => true,
'orderby' => 'tag_count',
'order' => 'DESC',
'tag__in' => $tags,
);
add_filter( 'posts_clauses', 'wpse173949_posts_clauses', 10, 2 );
$query = new WP_Query( $args );
remove_filter( 'posts_clauses', 'wpse173949_posts_clauses', 10 );
现在,搜索任何解决方案,以获得最相关的文章相同的标签计数后。。最有可能找到这样的解决方案:
$tags = wp_get_post_terms( $post -> ID, 'post_tag', ['fields' => 'ids'] );
$args = array(
'post__not_in' => array( $post -> ID ),
'posts_per_page' => -1,
'ignore_sticky_posts' => true,
'orderby' => 'count',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'terms' => $tags
)
)
);
$my_query = new wp_query( $args );
但是它不起作用。。我得到了带有ID的数组中的$tags
,
查询似乎也很有效,只是找不到任何类似的帖子。。。但我已经创建了和之类似的标签,并且与标签相同。。但结果仍然是0个帖子 在中找到了解决方案
所以我这样做:
function add_tags_categories() {
register_taxonomy_for_object_type('post_tag', 'posttype');
}
add_action('init', 'add_tags_categories');
$tags = wp_get_post_terms( $post -> ID, 'post_tag', ['fields' => 'ids'] );
$args = array(
'post_type' => 'posttype',
'post__not_in' => array( $post -> ID ),
'posts_per_page' => -1,
'ignore_sticky_posts' => true,
'orderby' => 'tag_count',
'order' => 'DESC',
'tag__in' => $tags,
);
add_filter( 'posts_clauses', 'wpse173949_posts_clauses', 10, 2 );
$query = new WP_Query( $args );
remove_filter( 'posts_clauses', 'wpse173949_posts_clauses', 10 );