Wordpress WP_Query with meta_query get values by Like

4.8k Views Asked by At

I'm building a custom search. On this i'm trying to get posts where the searched text should be used as a wild card to compare within 2 meta_key.

Below is my code,

<?php $args = (

    array(
        'post_type' => 'registration',
        'meta_key'=>'rg_upload_video',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'rg_first_name',
                'value' => $s,
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'rg_last_name',
                'value' => $s,
                'compare' => 'LIKE'
            )
        ),
        'posts_per_page' => 12,
        'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
    );

        $loop = new WP_Query($args);

?>

As a result it's simply returning all posts having Meta_key as rg_upload_video. I need the posts having searched text in meta_key rg_first_name or rg_last_name.

Did anyone know where i'm going wrong ?

2

There are 2 best solutions below

3
On

Try running the below code.

<?php $args =  array(
        'post_type' => 'registration',
        'meta_query' => array(
            'relation' => 'AND',
            array(
              'relation' => 'OR',
              array(
                'key' => 'rg_first_name',
                'value' => $s,
                'compare' => 'LIKE'
              ),
              array(
                'key' => 'rg_last_name',
                'value' => $s,
                'compare' => 'LIKE'
              )
            ),
            array(
               'key' => 'rg_upload_video',
               'compare' => 'EXISTS' 
            )
        ),
        'posts_per_page' => 12,
        'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
    );

        $loop = new WP_Query($args);

?>
2
On

You could do two WP_Query queries. In the first one you select all that has the rg_upload_video meta key. Then you do another WP_Query that searches only the posts obtained in the first query. Not tested, but it should look like:

$args = array(
    'post_type' => 'registration',
    'posts_per_page' => -1,
    'meta_key'=>'rg_upload_video',
);

$videos = new WP_Query($args);

# get list of posts containing meta key rg_upload_video
$post_ids = wp_list_pluck( $videos->posts, 'ID' );

# You then do another WP_Query to get the posts having postmeta first name or last nav_menu_description

$args = array(
    'post_type' => 'registration',
    'post__in' => $post_ids,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'rg_first_name',
            'value' => $s,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'rg_last_name',
            'value' => $s,
            'compare' => 'LIKE'
        )
    ),
    'posts_per_page' => 12,
    'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);

$loop = new WP_Query($args);