โพสต์ด้วยแท็กอย่างน้อย 3 รายการของแท็ก


13

ตัวอย่างเช่นมีแท็ก { foo, bar, chocolate, mango, hammock, leaf}

ผมอยากจะพบข้อความทั้งหมดที่มีอย่างน้อย 3 แท็กเหล่านี้

โพสต์ที่มีแท็ก { foo, mango, vannilla, nuts, leaf} จะตรงกับมันเพราะมันมี { foo, mango, leaf} - ดังนั้นอย่างน้อย 3 แท็กจากชุดที่จำเป็นของแท็ก

ดังนั้นมันจะอยู่ในรายการโพสต์ที่ตรงกัน

มีวิธีง่ายๆในการทำเช่นนั้นโดยไม่ต้องทำหลายลูปผ่านโพสต์ทั้งหมดหรือไม่

คำตอบ:


8

คำตอบด้านล่างนั้นง่ายขึ้นและสามารถขยายเพื่อตรวจสอบว่าโพสต์ใดมีแท็กที่ตรงกัน 3 แท็กก่อนที่จะแสดงรายการ ใช้หนึ่งแบบสอบถามและสมมติว่าคุณมีโพสต์อย่างน้อยหนึ่งรายการที่มี 3 แท็กที่ตรงกัน:

//List of tag slugs
$tags = array('foo', 'bar', 'chocolate', 'mango', 'hammock', 'leaf');

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

แก้ไข: การปรับตัวแปร$tag_min_matchจะกำหนดจำนวนการแข่งขัน


2

นี่เป็นวิธีหนึ่งในการทำ:

รับแท็ก 5 ชุด{a, b, c, d, e}:

1) ใน PHP สร้างชุดย่อยที่เป็นไปได้ทั้งหมดที่มี 3 องค์ประกอบโดยไม่มีการทำซ้ำ:

{a, b, c}
{a, b, d}
{a, b, e}
{a, c, d}
{a, c, e}
{b, c, d}
{b, c, e}
{c, d, e}

2) แปลงชุดย่อยเหล่านั้นเป็นเคียวรีอนุกรมวิธานขนาดใหญ่:

$q = new WP_Query( array(
  'tax_query' => array(
    'relation' => 'OR',
    array(
      'terms' => array( 'a', 'b', 'c' ),
      'field' => 'slug',
      'operator' => 'AND'
    ),
    array(
      'terms' => array( 'a', 'b', 'd' ),
      'field' => 'slug',
      'operator' => 'AND'
    ),
    ...
  )
) );

1

แนวทางของsprclldrคือวิธีที่ฉันใช้ สำหรับในขณะที่นี่คือสิ่งที่ฉันใช้แทน:

$relatedPosts = $tagged_posts->posts;
$indexForSort = array();

for ($i = count($relatedPosts) - 1; $i >= 0; $i--) {
  $relatedPostTags = get_tags($relatedPosts[$i]->ID);
  //get the ids of each related post
  $relatedPostTags = $this->my_array_column($relatedPostTags, 'term_id');
  $relatedPostTagsInPostTag = array_intersect($tags, $relatedPostTags);
  $indexForSort[$i] = count($relatedPostTagsInPostTag);
}

//sort by popularity, using indexForSort
array_multisort($indexForSort, $relatedPosts, SORT_DESC);

ฉันจะโพสต์ยอดนิยม:

$a_relatedPosts = array_slice($relatedPosts, 0, $this->numberRelatedPosts);

my_array_column เป็นฟังก์ชั่นที่คล้ายกับ array_column ของ PHP 5,5:

  protected function my_array_column($array, $column) {
    if (is_array($array) && !empty($array)) {
      foreach ($array as &$value) {
        //it also get the object's attribute, not only array's values
        $value = is_object($value) ? $value->$column : $value[$column];
      }
      return $array;
    }
    else
      return array();
  }

ไม่ตอบคำถามเริ่มต้น (แต่จะช่วยแก้ปัญหารากของฉัน ) เป็น: ถ้าไม่มีโพสต์ที่เกี่ยวข้องกับ 3 แท็กทั่วไปแล้วทั้งหมดนี้จะเหมือนกันให้บางโพสต์

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.