admin管理员组文章数量:1293660
I am trying to do a search engine, I am using get_posts () function with parameter 's'. It finds me post by title and content but in a disorderly way, how could I tell it to first consult the titles and then put at the end the post whose content is what I am looking for?
$query = array(
'post_type' => 'tacos',
'showposts' => 999,
'posts_per_page' => 999,
);
$query['s'] = "Fish";
$items = get_posts($query);
$posts = array();
foreach ($items as $position => $item) {...
This print the result of post with title and content that contains "Fish" but without order. I would like to put first title post and finally, the post with the content.
I finally solve it using specific sql query:
global $wpdb;
$query="SELECT
ID,
post_title
FROM $wpdb->posts
WHERE (`post_title`LIKE '%".$wpdb->esc_like($search)."%' OR `post_content`LIKE '%".$wpdb->esc_like($search)."%') AND `post_type`='page'
ORDER BY case
when `post_title`LIKE '%".$wpdb->esc_like($search)."%' then 0
when `post_content`LIKE '%".$wpdb->esc_like($search)."%' then 1
END, post_title";
$postid=$wpdb->get_results($query,'ARRAY_A');
本文标签: wp querySearch by post title and content in wpquery without order
版权声明:本文标题:wp query - Search by post title and content in wp_query without order 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741579060a2386463.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论