php - WordPress 元查询
问题描述
我正在尝试从数据库中获取带有post_title
like的帖子%Weapon%
。
我尝试了下面的代码,但single.php
死了。
$args = array(
'numberposts' => 20,
'category' => 4,
'meta_query' => array(
array(
'key' => 'title',
'value' => "Weapon",
'compare' => 'LIKE'
)
)
);
// $posts = get_posts($args);
query_posts( $args );
我想从post_title
类似的数据库中获取帖子%Weapon%
。
解决方案
如果使用“s”=>“Weapon”,它将为您提供标题和内容包含“Weapon”的所有帖子。
但是,如果您只想获得“类似标题”的帖子,则需要在此处添加一些 SQL。
$string="Weapon";
global $wpdb;
$theneededposts=$wpdb->get_col("SELECT ID FROM $wpdb->posts
WHERE post_title
LIKE '%".esc_sql($string)."%' "
);
if (empty($theneededposts)) $theneededposts=array();
$args = array(
'numberposts' => 20,
'category' => 4,
'post__in'=>$theneededposts,
);
$posts = get_posts($args);
推荐阅读
- schema - Jooq 模式名称作为查询参数
- html - 单选按钮填充
- android - Visual Studio 在 Android 应用中找不到扩展方法 Splat.BitmapMixins.FromNative() 和 Splat.BitmapMixins.ToNative()
- c++ - P1008(“禁止与用户声明的构造函数聚合”)在实践中何时有用?
- typescript - <Property X does not exist on type of import("...") > 如果是模块
- javascript - 如何在 javascript 中将 github oauth 2.0 与 http 连接起来
- python - 如何将 CuPy 数组传输到 tensorflow
- javascript - Highcharts - 如何在我的钟形曲线上绘制 X 轴线(标准偏差曲线)
- java - 生成随机数并推入数组而不重复
- kotlin - 如何判断两个数组是否具有相同的信息但在 Kotlin 中的顺序不同