我正在尝试从数据库中获得post_title
的帖子,例如%Weapon%
。
我尝试了Bellow的代码,但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" =>"武器",它将为您提供所有标题和内容包含"武器"的帖子。
但是,如果您只想获得"标题"帖子,则需要在此处添加一些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);
希望以下代码对您有帮助
<?php
global $wpdb;
$title = 'Weapon';
$myposts = $wpdb->get_col( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_title LIKE '%s'", '%'. $wpdb->esc_like( $title ) .'%' ) );
foreach ( $myposts as $mypost )
{
$post = get_post( $mypost );
//run your output code here
echo $post->post_title;
}
?>