問題描述

我剛從 4.2 升級到 4.4,現在我的分類查詢返回空。升級前一直工作正常。

我已經註冊了一個名為'title'的自定義分類,它由我的自定義帖子型別'sg-publications'使用。按照 WP 模板層次結構,我建立了一個名為 taxonomy-title.php 的模板,該模板使用預設查詢 args,直到現在,它已經透過標題正確顯示每個出版物。

以下是該模板中的 $ queried_object 和 $ wp_query-> 請求的輸出:

[queried_object] => WP_Term Object
    (
        [term_id] => 1256
        [name] => Stroupe Scoop
        [slug] => stroupe-scoop
        [term_group] => 0
        [term_taxonomy_id] => 1374
        [taxonomy] => title
        [description] => 
        [parent] => 0
        [count] => 30
        [filter] => raw
    )

[queried_object_id] => 1256

[request] => 
SELECT wp_posts.* 
FROM wp_posts 
INNER JOIN wp_term_relationships 
ON (wp_posts.ID = wp_term_relationships.object_id) 
WHERE 1=1 
AND wp_posts.post_title = 'stroupe-scoop' 
AND ( 
    wp_term_relationships.term_taxonomy_id 
    IN (1374)
    ) 
AND wp_posts.post_type = 'sg-publications' 
AND (wp_posts.post_status = 'publish' 
    OR wp_posts.post_status = 'private'
    ) 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date 
DESC 

在上述查詢中看到的問題就在 WHERE 1=1 之後,由於某些原因,它正在搜尋 post_title = 'stroupe-scoop'。這是不正確的 – 那就是分類術語俚語,而不是帖子的標題。實際上,當我註釋掉這行並且對資料庫執行它時,我得到了正確的回報。那麼什麼是導致 WP 新增該條件,什麼時候 (我假設) 在升級到 4.4 之前不新增它?

這裡是 taxonomy-title.php:

<?php
/**
 * @package WordPress
 * @subpackage Chocolate
 */
  global $wp_query;

  $quer_object = get_queried_object();
  $tax_desc    = $quer_object->description;
  $tax_name    = $quer_object->name;
  $tax_slug    = $quer_object->slug;

get_header();
get_sidebar();

$title = get_the_title( $ID );
$args  = array(
    'menu'            => 'new-publications',
    'container'       => 'div',
    'container_id'    => $tax_slug . '-menu',
    'menu_class'      => 'menu-top-style nav nav-tab',
    'menu_id'         => '',
    'echo'            => true,
    'fallback_cb'     => false,
    'before'          => '',
    'after'           => '',
    'link_before'     => '<i class="fa fa-chevron-circle-right fa-fw fa-2x"></i>',
    'link_after'      => '',
    'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    'depth'           => 0,
    'walker'          => ''
);

?>

<div id="page-title">
  <h1><?php _e( 'Publications - ' . $tax_name, LANGUAGE_ZONE ); ?></h1>
  <p><?php _e( 'View our monthly newsletter and stay informed on the latest real estate news.', LANGUAGE_ZONE ); ?></p>

<?php wp_nav_menu($args); ?>

</div>

<div id="multicol">

<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();

get_template_part( 'loop' , 'title' );

endwhile;
endif;
?>

</div><!-- end #multicol -->
<section class="page-text well"><?php _e( $tax_desc, LANGUAGE_ZONE ); ?></section>

<?php
get_footer();

而在 functions.php 我有這個查詢過濾器:

// use pre_get_posts to remove pagination from publications
function gd_publications_pagination( $query ) {
  if ( is_admin() || ! $query->is_main_query() )
    return;

  if ( is_tax('title') ) {
    // Display all posts for the taxonomy called 'title'
    $query->set( 'posts_per_page', -1 );
    return;
  }
}
add_action( 'pre_get_posts', 'gd_publications_pagination', 1 );

最佳解決方案

我不建議使用與公共查詢變數 (如 title) 一致的分類法。

title 查詢變數是在 4.4 中引入的,所以我認為可以解釋你的問題。

檢視 WP_Query 類的 this part

    if ( '' !== $q['title'] ) {
        $where .= $wpdb->prepare( 
            " AND $wpdb->posts.post_title = %s", 
            stripslashes( $q['title'] ) 
        );
    }

所以當我們使用例如:

example.tld/?title=test

WordPress 應該在這裡做什麼?是分類查詢還是標題搜尋?

所以我建議在自定義分類法中新增字首。

gary_title

以避免可能的名稱衝突。

更新:

感謝 @ ocean90 為 pointing out 這是一個錯誤,這將是 fixed 在 4.4.1

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。