Using:

$query = new WP_Query();

How do I filter to get records with titles OR content OR meta keys which contain a part of a string.

I currently have

$args=array(
          'order'=>'asc',
          'post_type' => $type,
          'post_status' => 'publish',
          'meta_query' => array(
                array(
                        'key'     => 'my_meta_key',
                        'value'   => sanitize_text_field($_GET["search"]),
                        'compare' => 'LIKE'
                    )
                ),
          'posts_per_page' => -1,
          'caller_get_posts'=> 1); 

Which is currently working perfectly but does not obvious filter by title and content as well.

4 Answers
4

Such condition is impossible to express in current WP API. You can use search for title/content matches and meta query for LIKE matches on post meta, but there is no way to establish OR relationship between the two.

The closest API way would be two–stage query where you query IDs for two sets of results (one for search, one for meta), then query full data for their combination.

Outside of that it is realm custom SQL. Not impossible, but relatively hard to write and harder to maintain.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *