简体   繁体   中英

wordpress font posts by tags count

Have custom post type posttype with multiple taxonomies like tax1 , tax2 and post_tag which added like this:

function add_tags_categories() {
    register_taxonomy_for_object_type('post_tag', 'posttype');
}
add_action('init', 'add_tags_categories');

And now searching for any solution to get most related posts by same tags count in posts.. most likely found solution like this:

$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 );

But its not working.. I got $tags in array with ids, query also seems working, just not finding any similar posts... but I have created and similar, and identical with tags also.. But result is still 0 posts..

Found solution in here

so I made like this:

$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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM