给定以下代码:
$loop = new WP_Query(
\'post_type\' => \'project\',
\'s\' => \'Test\',
\'posts_per_page\' => 10
);
var_dump($loop->request);
我希望
request
无论何时何地调用此代码,都要保持一致。相反,我看到它会产生不同的结果,这取决于它被调用的位置,我不清楚为什么会出现这种情况,因为参数没有改变。
目前,页面模板文件中的页面加载上存在一个调用。另一个调用存在于钩住wp_ajax_nopriv_[FOO]
和wp_ajax_[FOO]
操作(我在这里尝试使用它来构建Ajax的“加载更多”功能)。
了解原因吗WP_Query
如果根据调用的位置/时间生成不同的结果,将不胜感激。
以下是我的完整代码:
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(); ?>
以及
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\');
?>
以及
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
正在检查中的请求
page-work.php
产生:
检查$loop->request
在里面page-work.php
产生:
\'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\'...
检查
$loop->request
在里面
loadMore.php
产生:
\'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\'...