当用户的角色不同于时,我在查询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%\' )
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\');