Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#55037 new defect (bug)

get_posts why returning draft post list?

Reported by: sumitsingh's profile sumitsingh Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.9
Component: Posts, Post Types Keywords: has-screenshots reporter-feedback
Focuses: Cc:

Description

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
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();
}

Attachments (2)

image (13).png (1.0 MB) - added by sumitsingh 3 years ago.
Post listing SS
Posts ‹ Earthspas — WordPress.png (348.0 KB) - added by sumitsingh 3 years ago.
All post as a Draft excluding 1 post

Download all attachments as: .zip

Change History (10)

@sumitsingh
3 years ago

Post listing SS

@sumitsingh
3 years ago

All post as a Draft excluding 1 post

#1 @sumitsingh
3 years ago

  • Severity changed from major to critical

#2 @costdev
3 years ago

  • Focuses accessibility coding-standards removed

#3 @sumitsingh
3 years ago

  • Focuses accessibility coding-standards added

#4 @sumitsingh
3 years ago

  • Focuses coding-standards removed

#5 @sumitsingh
3 years ago

  • Focuses accessibility removed

#6 @costdev
3 years ago

Test Report

Env

  • Web Server: Apache
  • WordPress: 6.0-alpha-52448-src
  • Browser: Chrome
  • OS: Linux
  • Theme: Twenty Twenty-Two
  • Plugins: None activated

Steps to test

  1. Create two posts: One that is published and one that is saved as a draft.
  2. Add the contents of the first_post_ajax() function (not including function ...) in the ticket's description to a custom plugin or your theme.
  3. Make the following replacements:
$cid = 1; // Uncategorized
$dates = 'February-2022'; // Or whatever month-year it is for you.
$pages = 1;
  1. Remove the following block:
<div class="skider-img">
    <a href="'.get_the_permalink().'"><img class="img-responsive" src="'.$images[0].'" alt=""></a>
</div>
  1. Visit the frontend.

Results

  • Expected: The contents of both posts.
  • Actual: The contents of the published post.
  • Result: Could not reproduce ❌ The expected and actual values do not match.

Notes

  • Try disabling all plugins to see if the issue persists.
  • Try switching to a different theme to see if the issue persists.
  • If the issue does not persist, check for filters that may be applied to pre_get_posts or similar filters that may be causing an issue.
  • Alternatively, add post_status => 'publish' to the $args array.
Version 0, edited 3 years ago by costdev (next)

#7 @sumitsingh
3 years ago

Sorry we have call ajax with above mentioned first message

add_action ( 'wp_ajax_nopriv_first_post_ajax', 'first_post_ajax' );
add_action ( 'wp_ajax_first_post_ajax', 'first_post_ajax' );

#8 @audrasjb
3 years ago

  • Keywords reporter-feedback added
  • Severity changed from critical to normal

I can't reproduce this issue as well.
@sumitsingh please try to reproduce it on a fresh WordPress install, without any plugin and with a default theme to confirm the issue still occurs in that case.

Removing critical severity for now.

Note: See TracTickets for help on using tickets.