最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Search to include external content

programmeradmin1浏览0评论

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
  • Normally plugins maintain mirrors of the content in the form of posts so that they fit into the APIs, rather than requesting them at runtime, WP search is just WP_Query with an s 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
Add a comment  | 

1 Answer 1

Reset to default 0

For 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;
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论