wordpress - 仅在“pre_get_posts”钩子中查询带有特色图片的帖子
问题描述
我使用钩子“pre_get_posts”只查询在首页有特色图片的帖子:
add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $q ){
if ( $q->is_home() // only target homepage
&& $q->is_main_query() // only target the main query
&& !is_admin() // target front end only
) {
$q->set( 'meta_key', array( '_thumbnail_id' ) );
}
}
看起来这部分被忽略了。
$q->set( 'meta_key', array( '_thumbnail_id' ) );
感谢您的帮助。
解决方案
您需要检查 '_thumbnail_id' meta_key 是否存在。所以让我们像这样修改你的代码。
add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $q ){
if ( $q->is_home() // only target homepage
&& $q->is_main_query() // only target the main query
&& !is_admin() // target front end only
) {
$meta_query = array(
array(
'key'=>'_thumbnail_id',
'compare'=>'EXISTS',
),
);
$query->set('meta_query',$meta_query);
}
}
推荐阅读
- javascript - ; [HPM] 尝试代理请求时出错
- python - 如何使用 Python 和 BeautifulSoup 或 Pandas 从 Finviz.com 刮取 Top Gainers 和 Top Losers 的表格行?
- forms - Symfony 4 - 未单击时未请求复选框(通过 js 广告原型添加)
- python - Pandas 根据列表项重复行
- python - 在 django admin 中的 pre_save 上获取文件 mime 类型
- javascript - 以反应形式的角度处理选择标签的 onSelect 数据的最佳方法是什么
- python - 从python中的文本字符串中提取相关信息
- python - matplotlib 刻度与预先存在的刻度重叠
- java - 如何根据另一个列表中对象的字段值从列表中删除元素
- iis - 如何使用iis重写?