I am trying to get a post from database where post_title LIKE "%Weapon%".
I tried the code from bellow, but the single.php dies.
$args = array(
'numberposts' => 20,
'category' => 4,
'meta_query' => array(
array(
'key' => 'title',
'value' => "Weapon",
'compare' => 'LIKE'
)
)
);
// $posts = get_posts($args);
query_posts( $args );
I want to get posts from database where post_title LIKE "%Weapon%".
The problem is that the post title is not a meta value. As Artem suggested, have a look at this question, especially the answers from Tomás (which works, but will also search the post content) and Bullyen (using a custom query, searching only in the title): WordPress get_posts by title like
If to use "s"=>"Weapon" it will give you all posts that titles and contents contains "Weapon".
But if you want to get "title like" posts only, you need to add some SQL there.
$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);