[wp-trac] [WordPress Trac] #36687: Feature to override WP_Query to provide results from other source

WordPress Trac noreply at wordpress.org
Wed Jun 15 17:16:20 UTC 2016


#36687: Feature to override WP_Query to provide results from other source
-----------------------------+--------------------------
 Reporter:  jpdavoutian      |       Owner:  jpdavoutian
     Type:  feature request  |      Status:  closed
 Priority:  normal           |   Milestone:  4.6
Component:  Query            |     Version:
 Severity:  normal           |  Resolution:  fixed
 Keywords:  has-patch        |     Focuses:
-----------------------------+--------------------------

Comment (by jadpm):

 I could not replicate the very same issue on a new and clean installation.
 However, I am getting erratic outcomes on a fairly complex plugin that
 does custom queries which used to work properly before the changeset 37692
 and it is still not working with the latests SVN trunk version. Note that
 it does work properly on 4.5.2 and on SVN before that changeset.

 I did manage to replicate a small erratic behavior that might suggest that
 there are indeed side effects on those changes.

 Queries executed on AJAX calls are getting wrong `found_posts` and
 `max_num_pages` properties. On a local site I have, with just three posts,
 I am getting a count of 4 found posts, which is obviously wrong. The
 queries on my plugin, on the other hand, are getting 0 post founds.

 To replicate this small test case:
 * Install a SVN trunk copy of WordPress
 * Create 3 posts. I could reproduce the erratic behavior with also 5, 6
 and so on posts (the magic number seems to be 4...)

 Now you need an AJAX calback to generate a query:
 {{{#!php
 <?php
 function ek_query_callback() {
         $ek_args = array(
                 'post_type'             => 'post',
                 'posts_per_page'        => 2
         );
         $ek_query = new WP_Query( $ek_args );
         return '<pre>' . print_r($ek_query, true) . '</pre>';
 }

 add_action( 'wp_ajax_ek_get_posts', 'ek_get_posts' );

 function ek_get_posts() {
         $data = ek_query_callback();
         echo $data;
         die();
 }
 }}}

 And a place to fire the AJAX. Note that `ajaxurl` is not available by
 default on the frontend, so I hijacked the localization for a frontend
 script on the active theme to add it:

 {{{
 jQuery( document ).on( 'click', '.js-ek-get-posts', function( e ) {
         e.preventDefault();
         var data = {
                 action: 'ek_get_posts'
         };
         jQuery.ajax({
                 type:           "GET",
                 url:            ajaxurl,
                 data:           data
         }).done( function( response ) {
                 alert(response);
         });
 });
 }}}

 Create a page and add a fake link:

 {{{
 <a href="#" class="js-ek-get-posts">Get posts</a>
 }}}

 When you click the link, an alert should display the query data. Mind the
 `found_posts` and `max_number_pages` values. I am getting 4 and 2 for 3
 posts.

 Some odd combinations:
 * posts_per_page = 1 and 7 posts produces 2 found posts and 2 max pages.
 * posts_per_page = 2 and 7 posts produces 4 found posts and 2 max pages.
 * posts_per_page = 3 and 7 posts produces 6 found posts and 2 max pages.
 * posts_per_page = 4 and 7 posts produces 8 found posts and 2 max pages.

 As you can see, the number of found posts is random and sometimes higher
 than the actual number of existing posts, and the number of max pages
 seems to get stuck.

 So we do have a problem here.

 If the test case is too complex I can craft a Duplicator package with
 minimum changes in the TwentyFifteen theme to pack the AJAX calback, the
 JS for the frontend and the posts and pages used to test this. I can also
 pack this all as a standalone plugin if needed.

 Note that this is working properly, all of it, on 4.5.2 and on SVN before
 the first commit on this ticket.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/36687#comment:26>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list