Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search query parameters by title and meta

please help me create query parameters for search by title and meta, example:
item:
1 - title: company, content: asdf, meta_field_vendor: asd
2 - title: comp, content: company, meta_field_vendor: asd
3 - title: ttcmp, content: asdf, meta_field_vendor: asd
4 - title: myrus, content: asdf, meta_field_vendor: company

my search string ?s=company

I want to search result was items: 1,2,4

This qyuery arg

$args['wp_query'] = array(
    'post_type' => $post_type,
    'posts_per_page' => 5,
    's' => $search_s,
);

result 1 and 2

This qyuery arg

$args['wp_query'] = array(
    'post_type' => $post_type,
    'posts_per_page' => 5,
    'meta_query' => array (
        array(
            'key' => '_item_prop_title',
            'value' => $search_s,
            'compare' => 'EXISTS'
        )
    )
);

result 4

how can I make a query for result 1,3,4?

$args['wp_query'] = array(
    'post_type' => $post_type,
    'posts_per_page' => 5,
    's' => $search_s,
    'meta_query' => array (
        array(
            'key' => '_item_prop_title',
            'value' => $search_s,
            'compare' => 'EXISTS'
        )
    )
);

Cheers!

like image 497
SokoLBY Avatar asked Nov 01 '22 11:11

SokoLBY


1 Answers

what you could do for your problem is a dual process of search, if it is not an optimal response but if it works...

 $search_s = 'mykeyword';

    $q1 = get_posts(array(
        'post_type' => 'post',
        's' => $search_s
    ));

    $q2 = get_posts(array(
            'post_type' => 'post',
            'meta_query' => array(
                array(
                   'key' => 'my_meta_box',
                   'value' => $search_s,
                   'compare' => 'LIKE'
                )
             )
    ));

    $merged = array_merge( $q1, $q2 );

    $post_ids = array();
    foreach( $merged as $item ) {
        $post_ids[] = $item->ID;
    }

    $unique = array_unique($post_ids);

    $posts = get_posts(array(
        'post_type' => 'post',
        'post__in' => $unique,
        'post_status' => 'publish',
        'posts_per_page' => -1
    ));

    if( $posts ) : foreach( $posts as $post ) :
        setup_postdata($post);

        the_title();


    endforeach; 
    endif;
like image 190
Gabriel Intriago Avatar answered Nov 15 '22 06:11

Gabriel Intriago