1

What I want to achieve: A loop that display's all articles with a certain text string in their tags.

For example:

If I have an article tagged with "coffeemug" and one "coffeebean". Now I want to display them both because of the text string "coffee".

A default query like this:

query_posts( 'tag=coffee' );

will display none of it. It will just work for articles tagged exactly with "coffee"

And yes, I know, I can use an array for both tags. But in my case I don't know all tags which might be used in the future. I just know that some of them will start with "coffee". There can be hundreds of tags...or just the two above...but I want to catch 'em all.

1

Based on @birgire's always-useful comments, I did write a workaround for you. What I'm doing is to first find the terms that match the search string. Then, based on the found terms I will do a query in those.

function wpse_posts_by_tagname( $string ) {

    // Let's find every tag that has 'coffee' in it's name
    $term_args = array(
        'taxonomy'             => 'post_tag',
        'fields'               => 'ids',
        'name__like'           => $string,
        // 'description__like' => $string, // You can also search in description. 
        // 'search'            => $string, // We can even search in the term's name! 
    );
    $terms = get_terms( $term_args );

    // Let's make an array of term IDs
    if ( empty( $terms ) || is_wp_error( $terms ) )
        esc_html_e( 'No matches found', 'text-domain' );

    // Alright we got'em, now query based on these
    $query_args = array(
        'post_type'      => 'post',
        'tag__in'        => (array) $terms,
        'posts_per_page' => 10 // Optional limitation of posts per page
    );
    $tag_query = new WP_Query( $query_args );

    if( $tag_query->have_posts() ){
        while( $tag_query->have_posts() ){
            $tag_query->the_post();
            the_title( 
                sprintf( '<h2><a href="%s">', esc_url( get_permalink() ) ),
                '</a></h2>' 
            );
        }
        wp_reset_postdata();
    } else {
        esc_html_e( 'No matches found', 'text-domain' );
    }
}

Also it's worth noting that you should use WP_Query instead of query_posts. There are endless articles on the internet about this, so I'm just gonna skip this part.

| improve this answer | |
  • 1
    I don't think LIKE is supported as tax query operator and shouldn't it be post_tag ? – birgire Jul 20 '17 at 12:50
  • 1
    I guess many have tried the same, so you could mentioned that LIKE isn't supported. A workaround might be to find the terms with get_terms() using e.g. the name__like argument, and feed it to the WP_Query tax query? Or writing custom solutions via filters? Or better yet, suggest a proper taxonomy structure instead? – birgire Jul 20 '17 at 13:47
  • Thanks for the comments @birgire! Would you be kind and see if the update is written properly? – Jack Johansson Jul 21 '17 at 9:03
  • 1
    sure,I think the get_terms input argument should be a single array.and the_permalink() and the_title() echo but don't return the value. Resetting post data might be good too afterwards. Might also consider returning only ids from get_terms() withe 'fields' => 'ids'.. Ps: just to note that my suggestion might not scale well for huge number of terms. – birgire Jul 21 '17 at 14:57
  • 1
    Thanks for pointing out the issues, all set now. get_term does also have a string format (get_terms('taxonomy',$args)) but I suppose it's deprecated? Seems like I was quite sleepy while writing this, so many issues :P – Jack Johansson Jul 21 '17 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.