在相关的Post WordPress中排除草稿发布

我目前正在使用按类别过滤的相关帖子的自定义代码, 以显示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"> < ?phpecho "< h2> Related Health Hub Articles< /h2> "; ?> < /div> < ?phpwhile( $my_query-> have_posts() ) { $my_query-> the_post(); // display each post?> < div class="related-post-thumb col-sm-3"> < a href='http://www.srcmini.com/< ?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试试这个代码
< 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='http://www.srcmini.com/< ?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>

#2【在相关的Post WordPress中排除草稿发布】我已经审查了你的代码。你错过了post_status列。你想要的是状态为” 发布” 的任何内容。
请在$ args数组中添加’ post_status’ => ’ publish’ 。
你可以看到内置的WordPress函数。
http://codex.wordpress.org/Integrating_WordPress_with_Your_Website

    推荐阅读