• I tried to filter the sql produced by get_meta_sql(), following this article: https://wordpress.stackexchange.com/questions/178484/wp-query-args-title-or-meta-value

    It works… more or less. If I intoduce a tax query together with the meta query, the sql loose all the “meta” part.

    Without the tax query, the SQL produced is like the following:

    ["where"]=>
      string(395) " AND ( tm_posts.post_title like '{0acc5f40973efce47f8c42124c3734c97cc594f755a60224e0dd4cb02827c6b6}lorem{0acc5f40973efce47f8c42124c3734c97cc594f755a60224e0dd4cb02827c6b6}'  OR ( 
      ( tm_postmeta.meta_key = 'codice_linea' AND tm_postmeta.meta_value LIKE '{0acc5f40973efce47f8c42124c3734c97cc594f755a60224e0dd4cb02827c6b6}lorem{0acc5f40973efce47f8c42124c3734c97cc594f755a60224e0dd4cb02827c6b6}' )

    With the important tax query it becomes like this

    ["where"]=>
      string(177) " AND ( tm_posts.post_title like '{0406194437bac6b8187ce05f3bb4eecb83f0178715b7c8ca750a672ba53df8ac}lorem{0406194437bac6b8187ce05f3bb4eecb83f0178715b7c8ca750a672ba53df8ac}'  OR  ) " 

    As you all can see, all the part related to the meta query disappears.

    Why? ??

    These are part of the query args

    $args['tax_query'] = array( 'relation' => 'AND', 
                                array(
                                    'taxonomy' => 'stagione_linea',
                                    'field'    => 'slug',
                                    'terms'    => 'estate'
                                ) );
    
    $args['meta_query'] = array(
                            array(
                                'key' => 'codice_linea',
                                'value'=> $term,
                                'compare' => 'LIKE',
                            )
    );
    // this arg is added to activate the filter (see linked article)
    $args['_meta_or_title'] = $term;
    • This topic was modified 5 years, 3 months ago by koolmind.
    • This topic was modified 5 years, 3 months ago by koolmind.
    • This topic was modified 5 years, 3 months ago by koolmind. Reason: code missing
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The example code is setting the where clause using only title or meta query arguments. It does not consider that there may be other where clause elements.

    I don’t think there is a way to get other WHERE elements in the context of the ‘get_meta_sql’ filter. You could compose your own clause much like the example composes a title clause. The tax_query argument supplied to the original query would then have no bearing.

    It may be easier to use the ‘posts_where’ filter to str_replace() the appropriate AND operators with OR. This is inherently weaker than properly building clauses from query args, but I’m not sure you have a choice in this case.

    • This reply was modified 5 years, 3 months ago by bcworkz.
    Thread Starter koolmind

    (@koolmind)

    @bcworkz Thank you!
    I managed to style my query somehow…

    It’s not a good piece of code, but it works!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome! Hey, as long as it works. No one can see your code, only the results ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Searching in meta_value or title’ is closed to new replies.