admin管理员组文章数量:1334690
I am very new to Wordpress and PHP in general having mostly worked on .NET CMS systems in the past so please bare with me.
So, we have a Wordpress site that uses Advanced Custom fields, and Polylang amongst other plugins, to allow field based custom blocks that are fully translatable.
Some of our blocks might grab content from a shared source.. i.e. we will maintain a list of therapists (as its own custom content / page type) in one location, and when creating a treatment, we pick the related therapist, and in code, load the content from the therapist, and output this on the page.
Can anyone suggest how I can add search to include such content as described above?
I am very new to Wordpress and PHP in general having mostly worked on .NET CMS systems in the past so please bare with me.
So, we have a Wordpress site that uses Advanced Custom fields, and Polylang amongst other plugins, to allow field based custom blocks that are fully translatable.
Some of our blocks might grab content from a shared source.. i.e. we will maintain a list of therapists (as its own custom content / page type) in one location, and when creating a treatment, we pick the related therapist, and in code, load the content from the therapist, and output this on the page.
Can anyone suggest how I can add search to include such content as described above?
Share Improve this question asked Jun 8, 2020 at 12:38 mp3duckmp3duck 1011 bronze badge 1 |1 Answer
Reset to default 0For searching among your posts through Advanced custom fields, use fallowing query:
$args = [
'post_type' => 'post',
'meta_query' => [
'relation' => 'OR',
[
'key' => 'NAME_OF_ACF_FIELD',
'compare' => 'like',
'value' => '%'.$search_value.'',
]
]
];
$items = new WP_Query($args);
while($items->have_posts()) {
$items->the_post();
the_title();
}
This will output the titles that matches our search query.
even you can add more items to meta_query
array to search for example 3 field together.
Also, you can use WordPress filter hook to modify query before execution This is an example of modifying where statement on query
add_filter( 'posts_where', 'my_posts_where', 10, 2 );
function my_posts_where( $where, &$wp_query )
{
global $wpdb;
if ( $search_title= $wp_query->get( 'search_title' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( $wpdb->esc_like( $search_title) ) . '%\'';
}
return $where;
}
本文标签: Search to include external content
版权声明:本文标题:Search to include external content 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742365808a2461210.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
WP_Query
with ans
parameter, it isn't particularly sophisticated, has no indexing, and can't search for things other than posts out of the box – Tom J Nowell ♦ Commented Jun 8, 2020 at 13:39