WebDevB
WebDevB

Reputation: 492

Wordpress query in functions.php

I'm trying to run a query on a set of posts that have already been returned, I'm getting a query from the url using $_GET['location'] now the issue is this GET value can equal a multitude of different meta_values so i'm trying to run the below:

if( isset($_GET['location']) ) {
        $query->set('relation', 'OR');
        $query->set('meta_key', 'location_text');
        $query->set('meta_compare','LIKE');
        $query->set('meta_value', $_GET['location']);

        $query->set('relation', 'OR');
        $query->set('meta_key', 'job_description');
        $query->set('meta_compare','LIKE');
        $query->set('meta_value', $_GET['location']);

        $query->set('relation', 'OR');
        $query->set('meta_key', 'job_ref');
        $query->set('meta_compare','LIKE');
        $query->set('meta_value', $_GET['location']);

}

But returning the last query performed it only returns the last query so job_ref -> is this even possible?

Upvotes: 0

Views: 54

Answers (1)

Andy Tschiersch
Andy Tschiersch

Reputation: 3816

Looks like the last one overrides the first two (not sure). However, i think the follow will work:

$query->set( 'meta_query', array(
        'relation' => 'OR',
        array(
            'key'     => 'location_text',
            'value'   => $_GET['location'],
            'compare' => 'LIKE',
        ),
        array(
            'key'     => 'job_description',
            'value'   => $_GET['location'],
            'compare' => 'LIKE',
        ),
        array(
            'key'     => 'job_ref',
            'value'   => $_GET['location'],
            'compare' => 'LIKE',
        ),
    )
)

Upvotes: 1

Related Questions