admin管理员组

文章数量:1122832

i use below code for getting posts by word tag

$search_songs = array(
        'posts_per_page' => -1,
        'post_type' => 'songs',
        'tax_query' => array (
            array (
                'taxonomy' => 'post_tag',
                'field'    => 'slug',
                'terms'    => $search_query,
            )
        )
    );  

    $new_query = new WP_Query( $search_songs );
    $search_songs_posts = $new_query->posts;╨

But by this code it return posts that have exactly word tag

EXAMPLE : when search "akam" it return posts that have "akam" tag , BUT when search "aka" (a part of word) it return nothings i want to when search "aka" it return all post that have tags that include my arg("aka") not just exactly word

Thanks

i use below code for getting posts by word tag

$search_songs = array(
        'posts_per_page' => -1,
        'post_type' => 'songs',
        'tax_query' => array (
            array (
                'taxonomy' => 'post_tag',
                'field'    => 'slug',
                'terms'    => $search_query,
            )
        )
    );  

    $new_query = new WP_Query( $search_songs );
    $search_songs_posts = $new_query->posts;╨

But by this code it return posts that have exactly word tag

EXAMPLE : when search "akam" it return posts that have "akam" tag , BUT when search "aka" (a part of word) it return nothings i want to when search "aka" it return all post that have tags that include my arg("aka") not just exactly word

Thanks

Share Improve this question asked Nov 15, 2018 at 12:56 ebeliejinfrenebeliejinfren 1598 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You need to modify your query as:

$search_songs = array(
        'posts_per_page' => -1,
        'post_type' => 'songs',
        'tag' => $search_query,//use tag to filter posts
    );  

For more details

As per document here https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

If you are using tax query it only supports ‘IN’, ‘NOT IN’, ‘AND’, ‘EXISTS’ and ‘NOT EXISTS’.

So if you want to search "aka" and return posts with "akam" tag you need to do this using like query.

You can check this answer for reference : Wordpress tax query use operator LIKE

本文标签: wp querygetting posts by tags