Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a working query that calls all my posts that have a certain meta value for one of two meta keys:

$term = $_GET['term'];
$args = array(
    'post_type' => 'some_cpt_name',
    'meta_query' => array(
        'relation'      => 'OR',
        array(
            'key'     => 'foo',
            'value'   => $term,
            'compare' => 'LIKE',
        ),
        array(
            'key'     => 'bar',
            'value'   => $term,
            'compare' => 'LIKE',
        ),
    ) 
);
$query = get_posts( $args );

I need to query all posts that have the $term as meta key "foo" or "bar" or as post title. Problem is how to add the post title as additional possible key?

Q: How can I also check if the $term maybe is in the post_title?

share|improve this question

2 Answers 2

up vote -1 down vote accepted

As with most questions involving OR clauses, the answer is the same: use a custom query or alter WP_Query using the 'posts_clauses' filter.

share|improve this answer
    
Not really the answer as I was searching for the query string, but enough to be marked as answer. Thanks. –  kaiser Dec 29 '11 at 20:11

Ok, here's the final addition using the posts_clauses filter (and edit) as suggested from @scribu:

function alter_posts_where_clause( $where )
{
    global $wpdb;

    $term = trim( $_GET['term'] );
    $term = like_escape( $term );

    // Append to the WHERE clause:
    $where .= $wpdb->prepare( " OR {$wpdb->posts}.post_title LIKE '%s'", "%{$term}%" );

    return $where;
}
add_filter( 'posts_where', 'alter_posts_where_clause' );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.