Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按标签计数的wordpress字体帖子_Php_Wordpress_Tags - Fatal编程技术网

Php 按标签计数的wordpress字体帖子

Php 按标签计数的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

具有自定义的post type
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 );