排除相关Post Wordpress中的草稿帖子

时间:2018-03-30 07:47:03

标签: wordpress wordpress-theming

我目前正在使用相关帖子的自定义代码按类别过滤,显示4个相关帖子。 我的代码工作正常,但它也显示草稿帖子。理想情况下它应该而且有点令人沮丧。这是相关帖子的代码。

<div class="relatedposts">
<?php
// get current post categories and tags
$categories = get_the_category($post->ID);
$tags = get_the_tags($post->ID);
if ($categories || $tags) {
  $category_ids = array();
  if($categories)
  foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
  $tag_ids = array();
  if($tags)
  foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
  
  $args=array(
    'tax_query' => array(
      'relation' => 'OR',
      array(
        'taxonomy' => 'category',
        'field' => 'id',
        'terms' => $category_ids
      ),
      array(
        'taxonomy' => 'post_tag',
        'field' => 'id',
        'terms' => $tag_ids
      )
    ),
    'post__not_in' => array($post->ID),
    'posts_per_page'=> 4, // Number of related posts that will be shown.
  );
  // query posts
  $my_query = new WP_Query( $args );
  if( $my_query->have_posts() ) {
    ?>
    <div class="related-post-title">
    <?php
    echo "<h2>Related Health Hub Articles</h2>";
    ?>
    </div>
    <?php
    while( $my_query->have_posts() ) { $my_query->the_post();
      // display each post
      ?>
      <div class="related-post-thumb col-sm-3">
        <a href='<?php the_permalink(); ?>' rel='canonical' class="related-wrapper">
            <div class="related-thumb"><?php the_post_thumbnail(array(150,100)); ?></div>
            <h4 class="related-title"><?php the_title();?></h4>
        </a>
      </div>
      <?php
    }
  }
}
wp_reset_postdata();
?>
</div>

3 个答案:

答案 0 :(得分:0)

试试此代码

<div class="relatedposts">
<?php
// get current post categories and tags
$categories = get_the_category($post->ID);
$tags = get_the_tags($post->ID);
if ($categories || $tags) {
  $category_ids = array();
  if($categories)
  foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
  $tag_ids = array();
  if($tags)
  foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

  $args=array(
    'tax_query' => array(
      'relation' => 'OR',
      array(
        'taxonomy' => 'category',
        'field' => 'id',
        'terms' => $category_ids
      ),
      array(
        'taxonomy' => 'post_tag',
        'field' => 'id',
        'terms' => $tag_ids
      )
    ),
    'post__not_in' => array($post->ID),
    'posts_per_page'=> 4, // Number of related posts that will be shown.
    'post_status'      => 'publish'
  );
  // query posts
  $my_query = new WP_Query( $args );
  if( $my_query->have_posts() ) {
    ?>
    <div class="related-post-title">
    <?php
    echo "<h2>Related Health Hub Articles</h2>";
    ?>
    </div>
    <?php
    while( $my_query->have_posts() ) { $my_query->the_post();
      // display each post
      ?>
      <div class="related-post-thumb col-sm-3">
        <a href='<?php the_permalink(); ?>' rel='canonical' class="related-wrapper">
            <div class="related-thumb"><?php the_post_thumbnail(array(150,100)); ?></div>
            <h4 class="related-title"><?php the_title();?></h4>
        </a>
      </div>
      <?php
    }
  }
}
wp_reset_postdata();
?>
</div>

答案 1 :(得分:0)

我查看了您的代码。你错过了post_status专栏。任何具有状态&#34;发布&#34;是你想要的。

请在$ args数组中添加'post_status' => 'publish'

您可以看到内置的WordPress功能。

http://codex.wordpress.org/Integrating_WordPress_with_Your_Website

答案 2 :(得分:0)

$search_query=get_search_query();

function __extra_where($sql){
  global $wpdb;
  return ' AND '.$wpdb->prefix.'posts.post_status="publish" AND ( 1=1 '.$sql.' ) ';
}
add_filter( 'posts_where', '__extra_where', 10, 2 );

$query = new WP_Query(array(
  'post_status' => array( 'publish' ),
  'post_type' => ['post'/*your post_type here */],
  's'=>$search_query,
  'order' => 'ASC'
));
remove_filter( 'posts_where', '__extra_where', 10 );