为什么WP_QUERY对象中的QUERY_VARS会发生变化?

时间:2014-12-31 作者:Gixty

当用户的角色不同于时,我在查询WP\\U查询对象时遇到问题administrator

我的WP\\U查询如下:

function remove_new_c_post($author_id,$value_id) {

        $query = new WP_Query( array(
            \'post_type\' => \'custom_post_type\',
            \'author\' => $author_id,
            \'meta_query\' => array(
            \'relation\' => \'AND\',
                array(
                    \'key\'     => \'value_id\',
                    \'value\'   => $value_id,
                    \'compare\' => \'LIKE\',
                ),
            ),
            \'order\' => \'ASC\'

            ));

        if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
            wp_delete_post( get_the_ID() );
        endwhile;
        wp_reset_postdata();
        endif;

}
由于某种原因,当查询由管理员以外的用户运行时,结果如下:

WP_Query Object
(
    [query] => Array
        (
            [post_type] => custom_post_type
            [author] => 70
            [meta_query] => Array
                (
                    [relation] => AND
                    [0] => Array
                        (
                            [key] => value_id
                            [value] => 86
                            [compare] => LIKE
                        )

                )

            [order] => ASC
        )

    [query_vars] => Array
        (
            [post_type] => custom_post_type
            [author] => 86 //<--- THIS SHOULD BE 70
            [meta_query] => Array
                (
                    [relation] => AND
                    [0] => Array
                        (
                            [key] => value_id
                            [value] => 86
                            [compare] => LIKE
                        )

                ) 
            [order] => ASC
...
请注意中的作者query_vars 应该是70的时候是86。为什么要修改?

EDIT:

我刚刚尝试设置query\\u var$query->set( \'author\', $author_id ); 它保持了作者id的完整性;然而,mysql不断被修改:

  [request] => SELECT SQL_CALC_FOUND_ROWS  dlwp_posts.ID
               FROM dlwp_posts
               INNER JOIN dlwp_postmeta ON ( dlwp_posts.ID = dlwp_postmeta.post_id )   
               WHERE 1=1  AND dlwp_posts.post_author IN (86)
                 AND dlwp_posts.post_type = \'custom_post_type\'
                 AND (dlwp_posts.post_status = \'publish\'
                   OR dlwp_posts.post_status = \'future\'
                   OR dlwp_posts.post_status = \'draft\'
                   OR dlwp_posts.post_status = \'pending\'
                   OR dlwp_posts.post_status = \'private\')
                 AND (( dlwp_postmeta.meta_key = \'value_id\'
                   AND CAST(dlwp_postmeta.meta_value AS CHAR) LIKE \'%86%\' )

1 个回复
SO网友:shawncampbell

Milo\'s comment 关于pre_get_posts 帮我解决了这个问题。在我的例子中,父主题使用pre_get_posts 显式设置post_type 对于所有作者页。它不在函数中。php,但我可以通过在主题中搜索pre_get_posts. (我用过grep -R "pre_get_posts" . -l 从命令行中查找文件。)

如果您没有使用子主题,那么可以删除有问题的函数及其add_action().

如果使用的是子主题,要保持父主题的可升级性,可以使用remove_action 作用

Problematic Action existing in Parent Theme:

function problematic_parent_action( &$query ) {
  if ($query->is_author) {
    $query->set( \'post_type\', array( \'problematic_parent_post_type\' ) );
  }
}
add_action( \'pre_get_posts\', \'problematic_parent_action\' );

Resolution added to Child Theme:

function remove_problematic_parent_action() {
  remove_action( \'pre_get_posts\', \'problematic_parent_action\' );
}
add_action( \'init\', \'remove_problematic_parent_action\');

结束

相关推荐

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

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