0
votes

I am having trouble getting this piece of code working. If the syntax is incorrect I apologize. I am working with a client that had a theme already in place to use and I am trying to modify it accordingly. The client is a rental property manager and needs a single search freeform text field to loop through the address, city, state, and zip code to find a match or like match for the search input field. What I have so far does not work and just returns all properties no matter the search criteria. I am not particularly sure what I am doing wrong here and would appreciate any insight into this.

if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
 'post_type' => 'property',
 'paged' => $paged
);
if(isset($_GET['search_keyword'])) {
 if($_GET['search_keyword']) {
  $args['meta_query'][] = array(
   'relation'=>'or',
   array (
    'key' => 'pyre_address',
    'value' => $_GET['search_keyword'],
    'compare' => 'LIKE',
    'type' => 'CHAR'
   ),
   array(
    'key' => 'pyre_state',
    'value' => $_GET['search_keyword'],
    'compare' => 'LIKE',
    'type' => 'CHAR'
   ),
  array(
  'key' => 'pyre_city',
  'value' => $_GET['search_keyword'],
  'compare' => 'LIKE',
  'type' => 'CHAR'
 ),
 array(
  'key' => 'pyre_zip',
  'value' => $_GET['search_keyword'],
  'compare' => 'LIKE',
  'type' => 'CHAR'
 )
);

}
}

query_posts($args);
if(have_posts()):
1
Can you show the whole code, with get_posts or WP_Query calls? - Dima Knivets
I added that section of the coding, but I know that part isn't the issue. There are other advanced fields on this part that only call to check against one field in the database and they return the correct results just fine. - blckenedicekaj

1 Answers

1
votes

Just remove [] on your query:

$args['meta_query'] = array( // correct line, the [] has to be removed

FYI, specifying the type in char will cast the value to CHAR in the WHERE statement (see CAST. Since in Wordpress, in the table wp_postmeta, the column meta_value is in VARCHAR, I think it is not useful to specify it and it's just slowing down your query.

I hope I'm understandable :)

Also have a look on LIKE and difference between '%keyword%' and 'keyword'