admin管理员组文章数量:1122832
I've used a custom post type for one of my website. Custom post type contains scratch card data along with some custom fields.
I've developed an android application to manage those items from android device.
In the android app, I want to keep search feature which will help admin users to search card numbers to manage those.
I can use wordpress query to search by title.
Code
$args = array("post_type" => "mytype", "name" => $title);
$query = get_posts( $args );
It can only provide result if I provide exact title. But I need to retrieve all items with similar title.
Any suggestion?
I've used a custom post type for one of my website. Custom post type contains scratch card data along with some custom fields.
I've developed an android application to manage those items from android device.
In the android app, I want to keep search feature which will help admin users to search card numbers to manage those.
I can use wordpress query to search by title.
Code
$args = array("post_type" => "mytype", "name" => $title);
$query = get_posts( $args );
It can only provide result if I provide exact title. But I need to retrieve all items with similar title.
Any suggestion?
Share Improve this question edited May 6, 2024 at 7:20 Adrian 2851 gold badge3 silver badges11 bronze badges asked Feb 11, 2013 at 10:21 IFightCodeIFightCode 8032 gold badges12 silver badges26 bronze badges 1 |3 Answers
Reset to default 33You can use either search parameter of wp_query :
Code
$args = array("post_type" => "mytype", "s" => $title);
$query = get_posts( $args );
Or you can get posts based on title throught wpdb class:
global $wpdb;
$myposts = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_title LIKE '%s'", '%'. $wpdb->esc_like( $title ) .'%') );
Than you'll get post object in this form:
foreach ( $myposts as $mypost )
{
$post = get_post( $mypost );
//run your output code here
}
Much simpler nowadays:
$posts = get_posts([
'post_type' => 'recipe',
'title' => 'Chili Sin Carne',
]);
$posts
will contain all recipes with the exact title "Chili Sin Carne".
You can do something like this:
$posts = get_posts([
'post_type' => 'recipe',
'post_status' => ['publish', 'draft', 'private'],
's' => 'search string',
'search_columns' => ['post_title']
]);
This will search only the post_title
of the posts
本文标签: Query by quotsimilarquot post title
版权声明:本文标题:Query by "similar" post title 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308214a1933582.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_posts()
you can use the default search query ofWordPress
which will provide you the results of the searched term. It will also provide all the matching results with all your search terms. – Rohit Pande Commented Feb 11, 2013 at 10:44