0

In my custom search form, i'm currently using these kind of $args with my WP Query:

$args = array(

    'post_type'   => 'post',
    'posts_per_page'  => $postsPerPage,
    'offset'          => $postOffset,
    'category'        => 42,
    's'               => $name, //value of my searched term
    'orderby'         => 'date',
    'order'           => 'DESC',

);

I would like to search a post by "title" OR by "tag".

I've tried using this code:

$query_args = array(

'post_type' => 'post',

'post_status' => 'publish',

'order' => 'ASC',

'orderby' => 'none',

'posts_per_page' => '100',

'offset' => '2',

'page' => '2',

'tax_query' => array(

    '0' => array(

        'taxonomy' => 'post_tag',

        'field' => 'name',

        'terms' => array('test'),

        'operator' => 'IN',

    ),

    'relation' => 'OR',

),

'meta_query' => array(

    '0' => array(

        'key' => 'post_title',

        'value' => 'test',

        'compare' => 'LIKE',

    ),

    'relation' => 'OR',

),

but the result is empty.

Thanks!

2
  • WP_Query does not support searching by title or tag. Firstly, the 'relation' => 'OR' only applies to the meta queries and taxonomy queries individually. The meta query and taxonomy queries will always be AND. So you can't have meta query OR taxonomy query. Secondly, the post title is not meta, so cannot be queried with a meta query, Nov 12 '19 at 23:15
  • @JacobPeattie yes I know I can't search for "title" but right now my query is looking for title and description of posts, I don't know why but it works. I would like to looking for "tags" of posts, tha'ts why I was trying to use the relation function
    – Elle
    Nov 13 '19 at 9:27
0

Try getting rid of the "0" key in your tax_query and meta_query array.

$query_args = array(

    'post_type' => 'post',

    'post_status' => 'publish',

    'order' => 'ASC',

    'orderby' => 'none',

    'posts_per_page' => '100',

    'offset' => '2',

    'page' => '2',

    'tax_query' => array(

        array(

            'taxonomy' => 'post_tag',

            'field' => 'name',

            'terms' => array('test'),

            'operator' => 'IN',

        ),

        'relation' => 'OR',

    ),

    'meta_query' => array(

        array(

            'key' => 'post_title',

            'value' => 'test',

            'compare' => 'LIKE',

        ),

        'relation' => 'OR',

    ),

Also in your first set of args you have 'category' => 42, but this should be 'cat' => 42,

1
  • Mh, I will try it. Thanks anyway for your anwser
    – Elle
    Nov 20 '19 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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