search also in taxonomy, tags and custom fields

I have found many answers separately for Taxonomy, Custom fields and Tags, most of them outdated.

But how to combine a search in:

Read More
- designed custom fields of a custom post
- custom taxonomy terms
- Tags
- Title (already by default)
- Content (already by default)

One of my custom items could be for example:

item 1
tag: tag1, tag2, tag3
taxonomy: tax1, tax2
custom field 1: cf1
title: composed title 1
desc: a full paragraph with many words

Now I would like to be able to search for :

"paragraph tag2 cf1 composed"

The solution seems incredibly complicated… is it only possible with a new SQL query (JOIN LEFT etc) of can I find my way with simple $query->set() ?

add_filter( 'pre_get_posts', 'cjg_theme_filter' );

if (!function_exists('cjg_theme_filter')) {
    function cjg_theme_filter( $query ){

    if ( $query->is_main_query() ){
          //is_search is necessary for custom taxonomy urls
        if ( $query->get( 'tag' ) OR $query->get( 'artprim_categories' ) OR is_search() )
            $query->set( 'post_type', array( 'post', 'artprim_item' ) );

        if ( is_search() ){
            $queryString = $query->query_vars['s'];

            $queryTab = explode(' ', $queryString);

            $meta = array();
            foreach($queryTab as $queryString){
                $meta[] = array(
                    'key' => 'date',
                    'value' => $queryString,
                    'compare' => 'LIKE');
            }
            $query->set( 'meta_query',$meta );
        };        
    }

    return $query;

}}

Related posts

Leave a Reply

1 comment