Make WordPress Core

Opened 6 years ago

Closed 13 months ago

#42398 closed defect (bug) (invalid)

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

Reported by: solo14000's profile solo14000 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7
Component: Query Keywords: close
Focuses: Cc:

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 (4)

#1 @dd32
6 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
6 years ago

Related #24142

#4 @hellofromTonya
13 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

I agree with @dd32:

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.

Closing this ticket as it is the expected behavior.

Note: See TracTickets for help on using tickets.