skip to Main Content

I need help in getting related post using multiple tags.

I have Post with the tags poetry, motivational, attitude, rules, lines, sigma,inspirations

I wanted wp_query that matche most of these tags.

Note: Note every post has these all tags.

I am working on the site ntAfzal

2

Answers


  1. You can list all your post tags into an array and list the posts which belongs to that tags as below.

    $tags = array("poetry", "motivational", "attitude", "rules", "lines", "sigma", "inspirations");
    $query = new WP_Query(
        [
            'tax_query' => [
                [
                    'taxonomy' => 'post_tag',
                    'terms'    => $tags,
                    'compare'  => 'IN',
                ]
            ],
        ]
    );
    
    Login or Signup to reply.
  2. You can match most of the tags using tag__in.

    $post_tags = array("poetry", "motivational", "attitude", "rules", "lines", "sigma", "inspirations");
    
    $query = new WP_Query( array( 'tag__in' => $post_tags  ) );
    

    Here I added code for your reference and also you can see the documentation in WordPress for wp_query with tag parameters.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search