是否从相关帖子中排除一个或多个特定标签?

时间:2013-06-21 作者:Alex

如何在相关帖子的结果中排除特定标签的显示?

这是代码http://pastebin.com/Ksh7NY7g

/*-----------------------------------------------------------------------------------*/
/* Related Posts Function
/*-----------------------------------------------------------------------------------*/

function ag_get_related_post_args($id){

    global $relatedcatargs, $relatedargs;

    $rel_tagnames = \'\';
    $rel_catnames = \'\';

    if ( !($relatednumber = of_get_option(\'of_related_number\') ) ) { $relatednumber = \'2\'; } else { $relatednumber = of_get_option(\'of_related_number\'); } 

    // Get related post tag names
    $rel_tags = get_the_tags($id);

    if ($rel_tags) {
        // Get list of tag names and set arguments for loop
    foreach($rel_tags as $rel_tag) {
          $rel_tagnames .= $rel_tag->slug . \',\';
        }
        $relatedargs = array(
          \'ignore_sticky_posts\' => 1,
          \'tag\' => $rel_tagnames,
          \'post__not_in\' => array($id),
          \'showposts\' => $relatednumber,
          \'orderby\' => \'rand\'
        );
    }

    // Get list of category names and set arguments for loop
    $post_cats = wp_get_post_categories($id);

    foreach($post_cats as $post_cat) {
      $rel_catnames .= get_cat_name( $post_cat ) .\',\';
    }

    $relatedcatargs = array(
      \'ignore_sticky_posts\' => 1,
      \'category__in\' => $post_cats,
      \'post__not_in\' => array($id),
      \'showposts\' => $relatednumber,
      \'orderby\' => \'rand\'
    );

}

function load_custom_scripts() {
    global $wp_version;

    if (version_compare($wp_version, \'3.4.2\', \'>\')) {
        wp_register_script(\'my-jquery-ui\', \'https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js\', \'jquery\');
        } else {
        wp_register_script(\'my-jquery-ui\', \'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js\', \'jquery\');
    } 


    wp_enqueue_script( \'my-jquery-ui\' );
}

add_action(\'admin_init\', \'load_custom_scripts\'); 
global $relatedargs, $relatedcatargs;
// Query Posts by tag
$my_query = new WP_Query($relatedargs);

//If there are posts
if( $my_query->have_posts()) { ?>

<!-- Related Posts -->
<div class="relatedposts">
  <h3><?php _e(\'Related Posts\', \'framework\'); ?></h3>

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

  <a href="<?php the_permalink();?>" class="one_col half">
    <div class="featuredinner">
          <h2><?php 
              if (strlen($post->post_title) > 40) {
                  echo substr(the_title($before = \'\', $after = \'\', FALSE), 0, 80) . \'...\'; 
              } else {
                  the_title();
              }  ?>                      
              <span class="date"><?php the_time(get_option(\'date_format\')); ?> | <?php echo get_the_author(); ?></span></h2>
              <div class="featuredoverlay"></div>
              <?php the_post_thumbnail(\'blogonecol\', array(\'class\' => \'scale-with-grid\')); /* post thumbnail settings configured in functions.php */ ?>
    </div>
  </a>

    <?php endwhile;  ?>

  <div class="clear"></div>
</div>
<?php } wp_reset_query(); // end if there are posts and reset the query ?>

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

法典中有almost exactly what you are asking:

$args = array(
    \'post_type\' => \'post\',
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'movie_genre\',
            \'field\' => \'slug\',
            \'terms\' => array( \'action\', \'comedy\' )
        ),
        array(
            \'taxonomy\' => \'actor\',
            \'field\' => \'id\',
            \'terms\' => array( 103, 115, 206 ),
            \'operator\' => \'NOT IN\'
        )
    )
);
$query = new WP_Query( $args );
您只需删除几个不需要的部分,当然还要更改post_type 和分类详细信息,以适合您的数据。

$args = array(
    \'post_type\' => \'post\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'post_tag\',
            \'field\' => \'id\',
            \'terms\' => array( 103, 115, 206 ), // change these
            \'operator\' => \'NOT IN\'
        )
    )
);
$query = new WP_Query( $args );
问题中提供的最小细节是我所能做到的。

根据问题编辑中的新信息:

这个\'tag\' => $rel_tagnames, 您使用的模式是deprecated. 您应该使用tax_query 如上所述。因为您已经包含了要搜索的标记列表,所以不需要排除任何标记。当您包含特定标记时,其他标记将被排除在外。所以你想要的是:

if ($rel_tags) {
    $relatedargs = array(
        \'ignore_sticky_posts\' => 1,
        \'post__not_in\' => array($id),
        \'showposts\' => $relatednumber,
        \'orderby\' => \'rand\'
    );
    // Get list of tag names and set arguments for loop
    foreach($rel_tags as $rel_tag) {
      // exclude some tags
      $excluded_tags = array (1,2,3);
      if (in_array($rel_tag->term_id,$excluded_tags)) continue;
      $rel_tagnames[] = $rel_tag->term_id;
    }
    $relatedargs[\'tax_query\'] = array(
      array(
        \'taxonomy\' => \'post_tag\',
        \'field\' => \'id\',
        \'terms\' => $rel_tagnames,
      )
    )
}

// the rest of your code
我把它改成了IDs而不是slug。搜索这些可能比搜索蛞蝓更有效,尽管我还没有对其进行基准测试,并将其转换为tax_query. 构建“包含”列表时,会排除标记,而不是在查询本身中尝试这样做。

为什么要设置函数globals 而不仅仅是returning数据?

结束