0

My need is to have a WPQuery that searches for posts which title is 'test' OR author_name is 'test'.

Here is the code I tried:

$search_query = get_search_query();

$args = array(
    'relation' => 'OR',
    array(
        'key' => 'title',
        'value' => $search_query,
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'author_name',
        'value' => $search_query,
        'compare' => 'LIKE'
    )
);
$wp_query = new WP_Query($args);

I know that this code is meta_query style, but I'm trying to have OR in WP_Query

1 Answer 1

-1

EDIT : Sorry just read that you need a 'OR' in your query. I will update anwser soon.

According to your question, you can try this code.

// WP_Query arguments
$args = array(
    'name'                   => 'test',
    'author_name'            => 'test',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();

In the arguments, you should check if the parameters fits your needs :

  • 'name' : Display post by slugs
  • 'author_name' : Display posts by author 'user_nicename'

If you need help building your query, you should definetly refer to theses links :

Hope it help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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