在两个不同位置使用相同的参数调用WP_QUERY会产生两个不同的结果

时间:2017-04-26 作者:themarkappleby

给定以下代码:

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

1 个回复
SO网友:themarkappleby

事实证明,罪魁祸首是一个插件。我禁用了已安装的插件,然后分别重新启用它们,以识别名为Search Everything的插件(https://en-ca.wordpress.org/plugins/search-everything/) 作为原因。

看来,Search Everything正在修改WP_Query.

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post