WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 19 months ago

#42398 new defect (bug)

WP_Query with parameter post id 'p'=0 is not considered as a single post request

Reported by: solo14000 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7
Component: Query Keywords: close
Focuses: Cc:
PR Number:

Description

Hi there,

I'm trying to run a 'dummy' main WP query requesting only the post ID = 0 for performance purpose (all my requests are performed using AJAX with secondary extended WP queries)
However WP_Query does not detect a single post request adding SQL clauses like 'LIMIT'

In the class-wp-query.php file, the PHP test to set single post is

<?php
} elseif ( $qv['p'] ) {
        $this->is_single = true;

that of course fails with 'p' = 0

Is that an expected behavior ?

I'm using the following code

<?php
add_action('pre_get_posts', 'cb_pre_get_posts');

function cb_pre_get_posts($query){
$query->set('p', 0);
}

Thanks in advance,

Change History (3)

#1 @dd32
2 years ago

  • Keywords close added

Ignoring requests for p=0 seems like the expected behaviour to me, it's not a valid value for the parameter and thus isn't a single/singular request.

If you wish to avoid the default WP_Query query, you can use the posts_pre_query filter (see #36687) - code below isn't tested

add_filter( 'posts_pre_query', function( $val, $wp_query ) {
  if ( $wp_query->is_main_query() ) {
      return false; // any non-null value will cancel the request. Can also return an array of Post objects.
  }
  return $val;
}, 10, 2 );

#2 @birgire
2 years ago

Related #24142

Note: See TracTickets for help on using tickets.