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'...