0

I am trying to make the WP default search a bit more intelligent, by having it use custom fields. It searches through my custom post types, but only for the usual fields (excerpt, title, content).

In my arguments to WP_Query I specify all the post types. But not sure how to specify precisely all the custom fields with an OR because I am essentially looking for content in every post type.

The code below doesn't work, gives 0 results for the keyword quality for example. But a direct search in MySQL on the wp_postmeta table suggests there are 12 posts with the word quality in it. I want my query to bring out all of them.

What's the right way to include them in the WP_Query?

  1. No filters / actions
  2. No plugins I'd like to do this directly in WP_Query for now, and it needs to support two languages. I'll only ever need to query one language in each search engine (PolyLang).

The code:

$SEARCH = $_GET['s']; 

$args = array(
's' => $SEARCH
,'orderby' => 'relevance'
,'post_type' => array('page', 'services', 'resources', 'events', 'reports', 'posts')
,'post_status' => 'publish'
,'posts_per_page' => 15

,'meta_query' => array(
'relation' => 'OR'

// SEARCH THROUGH "reports" post type's custom fields
,array(
    'key'   => 'report_content',
    'value' => "$SEARCH",
    'compare' => 'LIKE'
    )
,array(
    'key'   => 'report_details',
    'value' => "$SEARCH",
    'compare' => 'LIKE'
    )

 // SEARCH THROUGH "events" post type's custom fields 
,array(
    'key'   => 'event_brief',
    'value' => "$SEARCH",
    'compare' => 'LIKE'
        )
    
)

);

What I am trying to do above is to look for the word in the variable $SEARCH inside those custom fields.

EDIT: Just to add, individually separate queries work. But instead of calling WP_Query three times, I'd like to combine them into one. I have checked through dozens of "I want to combine meta_query and tax_query" posts on Stack Overflow, but none of seem to be about combining different post_types in an OR condition, as one needs in a search through a website. The three that work individually:

//MAIN QUERY
$args = array(
      's' => $SEARCH
     ,'orderby' => 'relevance'
     ,'post_type' => array('page', 'services', 'resources', 'events', 'reports', 'posts')
     ,'post_status' => 'publish'
     ,'posts_per_page' => 15
);


// SEARCH THROUGH REPORTS
$args2 = array(
          'post_type'  => 'reports'
          ,'meta_query' => array(
              'relation' => 'OR',
              array(
                      'key' => 'report_content',
                      'value' => "$SEARCH",
                      'compare' => 'LIKE',
              ),
              array(
                      'key' => 'report_details',
                      'value' => "$SEARCH",
                      'compare' => 'LIKE',
              ),
          ),
      );

// SEARCH THROUGH EVENTS
$args3 = array(
            'post_type'  => 'events'
            ,'meta_query' => array(
                'relation' => 'OR',
                array(
                        'key' => 'event_brief',
                        'value' => "$SEARCH",
                        'compare' => 'LIKE',
                ),
                array(
                        'key' => 'event_instruction',
                        'value' => "$SEARCH",
                        'compare' => 'LIKE',
                ),
            ),
          );

But combining them this kludgish way:

$wp_query       = new WP_Query( $args);
$wp_query2      = new WP_Query( $args2);
$wp_query3      = new WP_Query( $args3);

$posts          = $wp_query->posts;
$posts2         = $wp_query2->posts;
$posts3         = $wp_query3->posts;

$posts          = array_unique( array_merge(
                    $posts
                    ,$posts2
                    ,$posts3
                    ) );
                    
                    

Does not work. Gives the dreaded error:

Uncaught Error: Object of class WP_Post could not be converted to string

Anyway, I'd like to merge these meta_query with an OR condition between them. I've found multiple meta_query examples, but they presume AND... as in all those meta_queries should be matched. That's not what I want. Many thanks.

1 Answer 1

0

Found a way to combine WP_Query posts:

$query1 = new WP_Query($args);
$query2 = new WP_Query($args2);
$query3 = new WP_Query($args3);

$combined = new WP_Query();

$combined->posts =   array_merge(
                           $query1->posts
                          ,$query2->posts
                          ,$query3->posts
                        );

Still not as elegant as doing one WP_Query with all those conditions in args being an OR ... as in the query should match ANY of those meta_query options.

Also, somewhow array_unique() wrapper to the array_merge() throws an error.

Your Answer

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

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