我正在开发一个使用艺术家自定义帖子类型的网站,我需要在页面模板中查询此类型的所有帖子。我需要设置is_post_type_archive 属性设置为true,以使用posts_orderby 滤器我正在使用以下代码,但WordPress似乎忽略了第三行:
$args = array(\'post_type\' => \'artist\');
$wp_query = new WP_Query( array_merge( $args, array(\'nopaging\' => true) ) );
$wp_query->is_post_type_archive = true;
这是过滤器:
function artist_posts_orderby( $orderby )
{
if (is_post_type_archive(\'artist\')) {
$orderby = "RIGHT(post_title, LOCATE(\' \', REVERSE(post_title)) - 1) ASC";
}
return $orderby;
}
add_filter(\'posts_orderby\', \'artist_posts_orderby\' );
有什么想法吗?
提前感谢
最合适的回答,由SO网友:leemon 整理而成
我重写了代码,现在它工作了
循环:
$args = array(\'post_type\' => \'artist\');
$wp_query = new WP_Query( array_merge( $args, array(\'nopaging\' => true) ) );
过滤器:
function theme_posts_orderby( $orderby, $query )
{
if ($query->query_vars[\'post_type\'] == \'artist\') {
// order artists by last name
$orderby = "RIGHT(post_title, LOCATE(\' \', REVERSE(post_title)) - 1) ASC";
}
return $orderby;
}
add_filter(\'posts_orderby\', \'theme_posts_orderby\', 10, 2 );