也许我的想法是错误的,但我需要:A)根据与meta\\u值的比较,排除网站范围内查询中的帖子(减去frontpage和navbar),b)按不同的meta\\u值,然后按标题排序
这项工作:
function my_get_posts( $query ) {
$today = current_time( \'timestamp\', 0 );
if ( ! is_admin() && ! is_home() && ! $query->is_page(\'Locations\')) {
$query->set( \'meta_key\', \'wpcf-post-weight\' );
$query->set( \'orderby\', \'meta_value_num title\' );
$query->set( \'order\', \'ASC\' );
$meta_query = array(
array(
\'key\' => \'wpcf-news-expire-date\',
\'value\' => $today,
\'compare\' => \'>=\'
)
);
$query->set( \'meta_query\', $meta_query );
}
return $query;
}
add_filter( \'pre_get_posts\', \'my_get_posts\' );
但是,自从
pre_get_posts
无法使用
is_front_page()
作为一个条件,我对如何排除头版有点困惑。将挂钩更改为
parse_query
, 然后使用
is_front_page()
要走的路是什么?
此外,我注意到调用此函数的页面不再具有由外观>菜单生成的导航菜单。我只能假设这是因为菜单是通过查询生成的。我不知道如何特别排除nav查询。
EDIT: 刚刚注意到这似乎适用于每个页面,但frontpage排除了导航菜单$query->query_vars[\'post_type\'] != \'nav_menu_item\'
是否有任何原因表明该函数可以访问除首页以外的每个页面上传递给WP\\U查询的query\\u vars?只有frontpage引发此错误:
<b>Notice</b>: Undefined property: WP_Query::$post in <b>...wp-includes/query.php</b> on line <b>3034</b><br />
<br />
<b>Notice</b>: Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3402</b><br />
<br />
<b>Notice</b>: Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3404</b><br />
<br />
<b>Notice</b>: Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3406</b><br />
<br />
<b>Notice</b>: Undefined property: WP_Query::$post in <b>...wp-includes/query.php</b> on line <b>3034</b><br />
<br />
<b>Notice</b>: Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3402</b><br />
<br />
<b>Notice</b>: Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3404</b><br />
<br />
<b>Notice</b>: Trying to get property of non-object in <b>...wp-includes/query.php</b> on line <b>3406</b><br />
<br />
<b>Notice</b>: Undefined index: post_type in <b>...lib/custom.php</b> on line <b>1051</b>
最后,。。。悲哀地
is_main_query()
就是不行。(我们发现自己遇到了一些棘手的情况,需要进行一些辅助查询。)
任何帮助或想法都将不胜感激。非常感谢。