2

Given the following code:

$loop = new WP_Query(
  'post_type' => 'project',
  's' => 'Test',
  'posts_per_page' => 10
);
var_dump($loop->request);

I would expect the produced request to look consistent regardless of where/when this code is invoked. Instead, I'm seeing it produce different results depending on where it is being invoked and it is unclear to me why this is the case given that the arguments do not change.

Currently, one invocation exists on page load in a page template file. The other invocation exists in a function that hooks into the wp_ajax_nopriv_[FOO] and wp_ajax_[FOO] actions (I'm trying to use it here to build out an Ajax "load more" feature).

Any insight into why WP_Query is producing different results based on where/when it is invoked would be greatly appreciated.

My code in full can be found below:

page-work.php

<?php
/**
 *  Template Name: Work
 *
 *  This is the template that displays the Work page.
 *
 */
  get_header();  
?>

<div class="index index--projects">
  <div class="container">
    <?php get_template_part('template-parts/search', 'box'); ?>
    <?php get_template_part('template-parts/filter', 'block'); ?>

    <div class="featuredPostsBlock">
      <div class="grid loadMore-target">
        <?php 
          $per_page = 10;
          $count = 0;
          $args = array(
            'post_type' => 'project',
            's' => $_GET['q'],
            'posts_per_page' => $per_page
          );
          $loop = new WP_Query( $args );
          while ( $loop->have_posts() ) : $loop->the_post(); 
            get_template_part('template-parts/content/content', 'featuredPostsBlock_work'); 
            $count++;
          endwhile;
          wp_reset_query();
        ?>
      </div>
      <?php if ($count == $per_page) : ?>
        <a class="btn-primary loadMore" data-type="project" data-ppp="1" data-search="<?php echo $_GET['q']; ?>" href="#">Load More</a>
      <?php endif; ?>
    </div>

  </div>
</div>
<?php get_footer(); ?>

and loadMore.php

<?php
  function load_more(){
    $offset = $_POST["offset"];
    $ppp = $_POST["ppp"];
    $search = $_POST["search"];
    $type = $_POST["type"];
    header("Content-Type: text/html");

    $args = array( 
      'post_type' => $type,
      's' => $search,
      'posts_per_page' => $ppp,
      'offset' => $offset
    );

    $loop = new WP_Query($args);
    while ($loop->have_posts()) : $loop->the_post(); 
      if ($type == "news") {
        get_template_part('template-parts/news/news', 'newsBlock'); 
      } else if ($type == "project") {
        get_template_part('template-parts/content/content', 'featuredPostsBlock_work'); 
      }
    endwhile;
    wp_reset_query();
    exit; 
  }

  add_action('wp_ajax_nopriv_load_more', 'load_more'); 
  add_action('wp_ajax_load_more', 'load_more');
?>

and loadMore.js

/* global $ */

function init () {
  $('.loadMore').click(function (event) {
    event.preventDefault()
    var count = $('.loadMore-target').children().length
    var search = $(this).data('search')
    var ppp = $(this).data('ppp')

    $.ajax({
      type: 'POST',
      url: 'http://test.dev/wp-admin/admin-ajax.php',
      data: {
        action: 'load_more',
        offset: count + 1,
        type: $(this).data('type'),
        search: search,
        ppp: ppp
      },
      success: function (posts) {
        $('.loadMore-target').append(posts)
        var html = $.parseHTML(posts)
        var results = $(html).children().length
        if (results < ppp) {
          $('.loadMore').hide()
        }
      }
    })
  })
}

module.exports = init

Inspecting the request in page-work.php produces:

Inspecting the $loop->request in page-work.php produces:

'SELECT DISTINCT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  LEFT JOIN wp_term_relationships AS trel ON (wp_posts.ID = trel.object_id) LEFT JOIN wp_term_taxonomy AS ttax ON (  ( ttax.taxonomy = 'category' OR ttax.taxonomy = 'post_tag' OR ttax.taxonomy = 'post_format' OR ttax.taxonomy = 'typology' OR ttax.taxonomy = 'location' )  AND trel.term_taxonomy_id = ttax.term_taxonomy_id) LEFT JOIN wp_terms AS tter ON (ttax.term_id = tter.term_id)  LEFT JOIN wp_comments AS cmt ON ( cmt.comment_post_ID = wp_posts.ID'...

Inspecting the $loop->request in loadMore.php produces:

'SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND (((wp_posts.post_title LIKE '%Test%') OR (wp_posts.post_excerpt LIKE '%Test%') OR (wp_posts.post_content LIKE '%Test%')))  AND wp_posts.post_type = 'project' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private')  ORDER BY wp_posts.post_title LIKE '%Test%' DESC, wp_posts.post'...
  • Can you post the page template and ajax function? Something else has to be involved here. A plugin or a filter in your theme's functions file. – jdm2112 Apr 26 '17 at 13:58
  • 1
    No problem, I've added the relevant code snippets to the question. – themarkappleby Apr 26 '17 at 14:09
  • How does request vary? What's different and where? Sounds like an incorrectly targeted filter being applied by some theme or plugin code. – Milo Apr 26 '17 at 15:55
2

It turns out it was a plugin that was the culprit. I disabled my installed plugins and then re-enabled them individually to identify a plugin named Search Everything (https://en-ca.wordpress.org/plugins/search-everything/) as the cause.

It seems, Search Everything was modifying the default behaviour of WP_Query.

  • Same here, I had a plugin called Intuitive Custom Post Order active, that does work on the WP_Query in a page, but not in fucntions (for use in ajax calls) – JosFabre Jun 21 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.