0

The basic function I'm trying to achieve is to get the tags of a current post, find all posts attached to those tags, but then only return posts that have at least 3 tags in common

I've been playing around with this for a week or so.

Found one snippet here that does the job almost perfectly.

$tags = array( 'bread', 'cheese', 'milk', 'butter');

$args = array(
    'tag_slug__in' => $tags
    // Add other arguments here
);

// This query contains posts with at least one matching tag
$tagged_posts = new WP_Query( $args );

echo '<ul>';
  while ( $tagged_posts->have_posts() ) : $tagged_posts->the_post();

   // Check each single post for up to 3 matching tags and output <li>
   $tag_count       = 0;
   $tag_min_match   = 3;

   foreach ( $tags as $tag ) {
      if ( has_tag( $tag ) && $tag_count < $tag_min_match ) {
         $tag_count ++;
      }
   }

   if ( $tag_count == $tag_min_match ) {
      //Echo list style here
      echo '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></li>';
   }

  endwhile;

wp_reset_query();
echo '</ul>';

But as you can see, you have to manually input tag names into the array...so it is doesn't work automatically.

I've tried a few different ways to try and pull the tags from the post automatically but can't get it to work...

My skill is rather limited and have only been able to make some clumsy steps into what turned out to be the wrong direction trying to put tag id's into arrays.

Does anybody have any idea how I could go about achieving this?

Pretty stumped.

0

So, if I'm reading your code correctly, you are trying to dump all the current posts' tag IDs into your tag__in array.

My only hesitation is I'm not sure exactly how the query returns the order, if it's just done by post date or actual tag relevance.

That should look something like this:

if ( $tags ) {

    $tag__in_array = wp_list_pluck( $tags, 'ID' );

    $args = array(
        'tag__in'             => $tag__in_array,
        'post__not_in'        => array( $post->ID ),
        'posts_per_page'      => 30,
        'ignore_sticky_posts' => true
    );
    $tags_query = new WP_Query( $args );
    if( $tags_query->have_posts() ) {

        // Rest is the same

Also, you'll want to use ignore_sticky_posts instead of caller_get_posts

caller_get_posts is deprecated since version 3.1!

Let me know if this works or not. Good luck!

  • Hey Socki thanks for taking interest in my little problem. I'm actually ordering posts by meta data value, the basic function I'm trying to achieve is to take posts – user121289 Jun 7 '17 at 17:20
  • continued: the basic function I'm trying to achieve is to get the tags of a current post, find all posts attached to those tags, but then only return posts that have at least 3 tags in common. I have removed my attempt from the OP as I feel it was a clumsy step in the wrong direction...although your code cleaned it up. – user121289 Jun 7 '17 at 17:26

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.