Make WordPress Core

Opened 2 years ago

Last modified 12 months ago

#60963 new defect (bug)

Custom query, inherit main query, show scheduled posts

Reported by: stefacchio Owned by:
Priority: normal Milestone: Awaiting Review
Component: Query Version:
Severity: major Keywords:
Cc: Focuses:

Description

Hi,
I created a custom query inheriting the main WP query, so my custom query has custom 'paged' args and others args are the same of the main query.
The problem is that the main query works fine, instead my WP query also show the Scheduled posts.
I debugged the args and are the same, I think there is a bug.

Here my code to explain better:

functions.php

<?php
global $wp_query;
wp_localize_script( 'ajax-frontend', 'ajaxfrontendobject', array(
                        'ajaxurl' => admin_url( 'admin-ajax.php' ),
                        'query_vars' => json_encode( $wp_query->query_vars ),
                        'security' => wp_create_nonce( 'get_posts_by_ajax' ),
                  ));

}}}

functions.php

<?php
add_action( 'wp_ajax_nopriv_ajax_pagination_get_posts', 'luxuryconcept_get_posts_by_ajax' );
add_action( 'wp_ajax_ajax_pagination_get_posts', 'luxuryconcept_get_posts_by_ajax' );

if ( ! function_exists( 'luxuryconcept_get_posts_by_ajax' ) ) {
        function luxuryconcept_get_posts_by_ajax() {
                check_ajax_referer('get_posts_by_ajax', 'security');

                $args = json_decode( stripslashes( $_POST['query_vars'] ), true );
                $args['paged'] = $_POST['page'];
                query_posts( $args );


            echo "<br><br> ajax:" . print_r($GLOBALS['wp_query']->query_vars);

                
                get_template_part( 'templates/loop', 'index' ); 

                die();
        }
}

JS

$.ajax({
			url: ajaxfrontendobject.ajaxurl,
			type: 'post',
			data: {
				action: 'ajax_pagination_get_posts',
                                security: ajaxfrontendobject.security,
				query_vars: ajaxfrontendobject.query_vars,
				page: page
			},

Thanks

Attachments (2)

get_posts_main_query_no_ajax.jpg (350.5 KB ) - added by stefacchio 2 years ago.
get post with main query without ajax
get_posts_inherit_custom_query_ajax.jpg (470.5 KB ) - added by stefacchio 2 years ago.
get post with custom query inheriting main query using ajax

Download all attachments as: .zip

Change History (3)

@stefacchio
2 years ago

get post with main query without ajax

@stefacchio
2 years ago

get post with custom query inheriting main query using ajax

#1 @SirLouen
12 months ago

  • Component Posts, Post TypesQuery
  • Version 6.5
Note: See TracTickets for help on using tickets.