[wp-trac] [WordPress Trac] #55037: get_posts why returning draft post list?

WordPress Trac noreply at wordpress.org
Wed Feb 2 05:04:02 UTC 2022


#55037: get_posts why returning draft post list?
---------------------------------------------+-----------------------------
 Reporter:  sumitsingh                       |      Owner:  (none)
     Type:  defect (bug)                     |     Status:  new
 Priority:  normal                           |  Milestone:  Awaiting Review
Component:  Posts, Post Types                |    Version:  5.9
 Severity:  major                            |   Keywords:  has-screenshots
  Focuses:  accessibility, coding-standards  |
---------------------------------------------+-----------------------------
 Hi,

 I have created one website & post in **Draft** but still gatting on
 website when visit as a visitor. you can click on post for go to details
 page then gettot 404 erorr page.

 Website URL is here - https://earthspas.com/news-blog/

 My code below.

 {{{#!php
 <?php
 add_shortcode('blog-listing', 'blog_listing_function');
 function first_post_ajax() {
         $latest_cpt = get_posts("post_type=post&numberposts=1");
         $latestpost =  $latest_cpt[0]->ID;
         $cid = $_POST['catid'];
         $dates = $_POST['dates'];
         $pages = $_POST['page'];
         $datesArray = explode('-', $dates);
         $datemonth = date_parse($datesArray[0]);
         if($pages){
                 $pages = $pages;
         }else{
                 $pages = 1;
         }
         if($cid != 'all'){
                 $tax_query = array(
                                 array(
                                         'taxonomy' => 'category',
                                         'field' => 'id',
                                         'terms' => $cid
                                 )

                         );
         }else{
                 $tax_query = '';
         }
         if($dates != 'all'){
                 $date_query = array(
                                    'relation' => 'AND',
                                 array(
                                         'year'  => $datesArray[1],
                                         'month' => $datemonth['month'],
                                         'compare'   => '=',
                                 ),
                         );
         }else{
                 $date_query = '';
         }
         $args = array(

                 'post_type' => 'post',
                 'orderby' => 'ID',
                 'post__not_in' => array($latestpost),
                 'order' => 'DESC',
                 'posts_per_page' => 6,
                 'paged' => $pages,
                 'tax_query' =>$tax_query,
                 'date_query' =>$date_query
         );

                 $the_query = new WP_Query( $args );
                 $totalPage = $the_query->max_num_pages;
                 if ( $the_query->have_posts() ) :
                 while ( $the_query->have_posts() ) :
 $the_query->the_post();
                 $images = wp_get_attachment_image_src(
 get_post_thumbnail_id( $the_query->ID ), 'single-post-thumbnail' );
                         $html .='<div class="single-blog">
                                 <div class="skider-img">
                                         <a
 href="'.get_the_permalink().'"><img class="img-responsive"
 src="'.$images[0].'" alt=""></a>
                                 </div>
                                 <div class="item-details">
                                         <p>'.get_the_title().'...</p>
                                         <p class="blog-
 date">'.get_the_date().'</p>
                                 <a href="'.get_the_permalink().'" class
 ="read-more-link">READ MORE <svg width="27" height="18" viewBox="0 0 27
 18" fill="none">
                                 <path d="M18 18L18.0608 17.1529L25.0792
 8.89404L26.0514 8.89404L26.0514 9.13228L18.79 17.8941L18 18Z"
 fill="#4d4d4d"></path>
                                 <path d="M18 0L18.0608 0.847066L25.0792
 9.10596L26.0514 9.10596L26.0514 8.86772L18.79 0.105883L18 0Z"
 fill="#4d4d4d"></path>
                                 <path d="M0 8.5H25V9.5H12.5H0V8.5Z"
 fill="#4d4d4d"></path></svg></a>
                                 </div>
                     </div>';
                 endwhile;
                 endif;
                         wp_reset_postdata();
                         wp_reset_query();

                         if($totalPage >1){
                 $html .='<ul>';
                         if($pages > 1 ){
                         $j = $pages-1;
                         $html.='<li><a href="javascript:void(0)" data-
 page="'.$j.'" class="next-page blog-pagination"><i class="fas fa-arrow-
 left"></i>Prev</a></li>';
                         }
                         for($i=1; $i<=$totalPage; $i++){
                                 if($i == $pages){
                                         $activeClass = 'active';
                                 }else{
                                         $activeClass = '';
                                 }
                         $html.='<li><a href="javascript:void(0)" data-
 page="'.$i.'" class="blog-pagination '.$activeClass.'">'.$i.'</a></li>';
                         }
                         if($pages < $totalPage ){
                         $j = $pages+1;
                         $html.='<li><a href="javascript:void(0)" data-
 page="'.$j.'" class="next-page blog-pagination">Next<i class="fas fa-
 arrow-right"></i></a></li>';
                         }
                 $html.='</ul>';
                         }
                 echo $html;

 die();
 }

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/55037>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list