Wordpress Search through multiple database fields - wordpress

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()):

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'

Related

WordPress get_users meta_query search object value

I'm running a query on users and want to include only users who have meta data (page_history) with a specific value. The problem I'm having is that the meta data is an object, and I can't seem to get the query to check the value of the object:
$role = isset($_GET['role'] && $_GET['role'] !== '');
$args = array(
'role' => $role ?? '',
'orderby' => 'user_nicename',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
$search_term[0] ? array(
'key' => 'first_name',
'value' => $search_term[0],
'compare' => 'LIKE'
) : '',
$search_term[1] ? array(
'key' => 'last_name',
'value' => $search_term[1],
'compare' => 'LIKE'
) : '',
array(
'key' => 'page_history',
'value' => 'custom_post_type',
'compare' => 'EXISTS'
)
),
);
$users = get_users($args);
foreach ($users as $user) {
// Do stuff
}
Here is my meta object:
Array
(
[date] => 1655387457
[post-type] => custom_post_type
[page] => Page title here
)
So I just want to get users if they have the meta data page_history set as 'custom_post_type'.
Thank you
This meta object will be serialized before being stored in the database column.
The post-type field after serialization will be like that in DB s:9:"post-type";s:16:"custom_post_type";
You can use LIKE compare to filter it.
array(
'key' => 'page_history',
'value' => 's:9:"post-type";s:16:"custom_post_type";',
'compare' => 'LIKE'
)
You will need to change the number 16 to the custom post type name count.

Is there an way 'We pass $_Get values into an array'

I am getting url string,
product_cat=booking&min_prep_time=180&max_prep_time=220
and i am passing the values into array with help of $_GET. Recently i modified the code in my custom file for tax_query and meta_query, these work fine for me but according to my custom requirement i changed the static values into dynamic variable seems like this
when i used the static value the array is perfectly working and fetched perfect data
Before Array with static values was working
$meta_query[] = array(
'key' => '_new_field',
*'value' => array( '19','22'),*
'compare' => 'between',
);
After Its not working
$meta_query[] = array(
'key' => '_new_field',
*'value' => array( $min,$max),*
'compare' => 'between',
);
This is my complete code i am sure this will help you to understand my problem
$prod = isset($_GET['product_cat']) ? $_GET['product_cat'] : FALSE;
$min = isset($_GET['min_prep_time']) ? $_GET['min_prep_time'] : FALSE;
$max = isset($_GET['max_prep_time']) ? $_GET['max_prep_time'] : FALSE;
if( $prod && ($prod=="booking") && $min && $max) {
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( $prod ), // Don't display products in the clothing category on the shop page.
'operator' => 'IN'
);
$meta_query = (array)$q->get('meta_query');
// Add your criteria
$meta_query[] = array(
'key' => '_new_field',
'value' => array( $min,$max),
'compare' => 'between',
);
// Set the meta query to the complete, altered query
$q->set('meta_query',$meta_query);
$q->set('tax_query', $tax_query);
What i want
i just want i passed the values that i get from url and array will perfectly working as it work for static values.
many thanks

Multiple custom field sorting on wordpress archive page

I have been through several topics on sorting taxonomies and custom fields, including some that were promising. However, I am still not constructing my query args correctly it seems.
I am "fixing" someone else's code in a custom theme that is used in a multisite configuration, but I am able to override/filter the query on a per-site basis.
The query is called via the theme with the following:
return apply_filters( 'theme_query_args_filter', $args);
I have custom fields in the "role" taxonomy that include:
last_name
first_name
Pretty common I think.
However, when the args are executed, the following filter args are sorting only by last name (key part is the else clause):
function my_args_filter( $args ) {
if (is_home() && !is_search()) {
$args['meta_key'] = 'event_date';
$args['orderby'] = 'event_date';
$args['order'] = 'DESC';
}
else {
$tax = $args['taxonomy'];
$theterm = $args['term'];
$args = array (
'taxonomy' => $tax,
'term' => $theterm,
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
}
add_filter( 'theme_query_args_filter', 'my_args_filter' );
I've tried to modify the orderby as indicated in https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/ to use an array to do a multisort, but I'm hitting my head up against the wall. I think the problem is that the code is written using a mixture of old ways of doing things and new ways.
Any advice is appreciated. According to the example on in the docs above, I SHOULD be able to pass in multiple meta key/value/orders via an array, but I'm just not getting it.
Thanks for any leads you might have. (long-time listener, first-time caller)
(I also looked at https://wordpress.stackexchange.com/questions/109849/order-by-desc-asc-in-custom-wp-query but I couldn't extrapolate that example to this one either)
Figured it out on my most recent try. Hat tip to this solution https://wordpress.stackexchange.com/questions/249881/multiple-custom-fields-for-orderby-in-wp-query by Nath. if there is a more elegant way to do it, please let me know.
$tax = $args['taxonomy'];
$theterm = $args['term'];
$paged = $args['paged'];
$args = array (
'taxonomy' => $tax,
'term' => $theterm,
'paged' => $paged,
'posts_per_page' => '10',
'meta_query' => array(
array(
'relation' => 'AND',
'last_name' =>
array(
'key' => 'last_name',
'compare' => 'EXISTS',
),
'first_name' =>
array(
'key' => 'first_name',
'compare' => 'EXISTS',
),
),
),
'orderby' => array (
'last_name' => 'ASC',
'first_name' => 'ASC',
)
);
}

WP_Query meta_query argument

I am trying to write a 'complex' WP_Query but it keeps failing. The meta_query argument Im using is:
$query_args['meta_query'] = array(
"relation" => "AND",
array("relation" => "OR",
array(
'key' => '_htp_hide_trending_posts',
'value' => 'on',
'type' => 'CHAR',
'compare' => '!='
),
array(
'key' => '_htp_hide_trending_posts',
'compare' => 'NOT EXISTS'
)
),
array(
'key' => 'views',
'value' => 0,
'type' => 'NUMERIC',
'compare' => '>'
)
);
_htp_hide_trending_posts is a custom meta_value. If I ignore the views part and just use the OR statement, it's ok but adding the ADD relation and it then ignores the OR relation search. I need both as it needs to order by views and that doesn't work if it's not in the meta_query. I'm going round in circles, hoping someone can spot an obvious mistake?
I finally worked out a solution:
$query_args['meta_query'] = array(
array(
'key' => '_htp_hide_trending_posts',
'compare' => 'NOT EXISTS'
)
);
$query_args['meta_key'] = 'views';
$query_args['meta_compare'] = '>';
$query_args['meta_value'] = '0';
$query_args['orderby'] = array('meta_value_num' => 'DESC');
I didn't need the OR query which seemed to do the trick.

Wordpress query with sub relations

Is there a way to have a meta_query with sub relations? I don't want one relation to apply to all of the query's arrays.
I tried this, but to no avail:
'meta_query' => array(
array(
'key' => 'event_type',
'value' => 'session',
'compare' => 'LIKE'
),
array(
'relation' => 'OR',
array(
'key' => 'event_video',
'value' => '0',
'compare' => '>'
),
array(
'key' => 'event_summary',
'value' => '',
'compare' => '!='
)
),
array(
'key' => 'event_date_single',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'event_start_time',
'value' => '',
'compare' => 'LIKE'
)
)
So 'relation' => 'OR' should only apply between event_video and event_summary. Everything else is independent of one another. Is this possible somehow?
Thanks
It looks like this could be a meta_query limitation. WP Codex shows that you can use relations for meta queries, but as far as I see the limitation is that you can only use one kind of relation for the whole meta_query.
This means you can have a full OR or a full AND (default) relation between meta_query parameters, but you can never have them both combined, which is just what you want.
As I was not glad when I faced it at WP Codex, I went deeper in the matter by manually searching the code. So I found out that WP_Query calls WP_Meta_Query to parse meta_query parameters. This is WP_Meta_Query's class constructor (you can find it in wp-includes/meta.php at line 643 in WP 3.6):
/**
* Constructor
*
* #param array $meta_query (optional) A meta query
*/
function __construct( $meta_query = false ) {
if ( !$meta_query )
return;
if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
$this->relation = 'OR';
} else {
$this->relation = 'AND';
}
$this->queries = array();
foreach ( $meta_query as $key => $query ) {
if ( ! is_array( $query ) )
continue;
$this->queries[] = $query;
}
}
Long story short, the second if shows that this class will work only either with a OR or AND relation, but never with both of them combined.
So, unfortunately, it seems that it's not possible to achieve what you are trying to do (at least until WP 3.6). I guess your best shot is to split the main query, first doing specific Meta Queries and then applying their results(returned metas) to your main query.
I know that this is not a solution but an answer. Nevertheless, I hope this may help you to find a solution for this matter.

Resources