Slow speed Search Query WordPress - php

I have this query when I decrease filters its become fast but as I increase filters its become too slow, Please I need to make it fast with all these filters.
As my details are clears but I am writing some lines because its does not allow me to post my Question
$args = array('post_type' => 'property', 'posts_per_page' => 1,'order' => 'asc',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'facilityname',
'value' => $_GET['keyword'],
'compare' => 'LIKE'
),
array(
'key' => 'city',
'value' => $_GET['city'],
'compare' => 'LIKE'
),
array(
'key' => 'facilitytype',
'value' => $_GET['facilitytype'],
'compare' => 'LIKE'
),
array(
'key' => 'hospitaltype',
'value' => $_GET['hospitaltype'],
'compare' => 'LIKE'
),
array(
'key' => 'state',
'value' => $_GET['state'],
'compare' => 'LIKE'
),
array(
'key' => 'idn',
'value' => $_GET['idn'],
'compare' => 'LIKE'
),
array(
'key' => 'gpoaffiliations',
'value' => $_GET['gpoaffiliations'],
'compare' => 'LIKE'
),
array(
'key' => 'bedcounttotal',
'value' => array($_GET['min-bedcounttotal'], $_GET['max-bedcounttotal']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'zipcode',
'value' => array($_GET['minzipcode'], $_GET['maxzipcode']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'operatingroomcount',
'value' => array($_GET['minoperatingroomcount'], $_GET['maxoperatingroomcount']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
)
);

Finally I found a solution, I am sharing it here for other may have same problem Like me
$meta_query = array();
$args = array();
$args = array('posts_per_page' => -1,);
$meta_query[] = array(
'key' => 'facilityname',
'value' => $_GET['keyword'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'city',
'value' => $_GET['city'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'facilitytype',
'value' => $_GET['facilitytype'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'hospitaltype',
'value' => $_GET['hospitaltype'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'state',
'value' => $_GET['state'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'idn',
'value' => $_GET['idn'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'gpoaffiliations',
'value' => $_GET['gpoaffiliations'],
'compare' => 'LIKE'
);
$meta_query[] = array(
'key' => 'bedcounttotal',
'value' => array($_GET['min-bedcounttotal'], $_GET['max-bedcounttotal']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
);
$meta_query[] = array(
'key' => 'zipcode',
'value' => array($_GET['minzipcode'], $_GET['maxzipcode']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
);
$meta_query[] = array(
'key' => 'operatingroomcount',
'value' => array($_GET['minoperatingroomcount'], $_GET['maxoperatingroomcount']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
);
//if there is more than one meta query 'or' them
if(count($meta_query) > 1) {
$meta_query['relation'] = 'AND';
}
// The Query
$args['post_type'] = "property";
$args['meta_query'] = $meta_query;

Related

Complex Meta Query not returning wanted results

$tax_query = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $ajs_cat,
)
);
$args = array(
'post_type' => 'post',
'orderby' => 'menu_order',
'posts_per_page' => $per_page,
'paged' => $paged,
'tax_query' => $tax_query,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'price_min',
'value' => array($min_price, $max_price),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
),
array(
'key' => 'price_max',
'value' => array($min_price, $max_price),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
),
)
);
I am trying to return acts based on a searched budget minimum (price_min) and maximum (price_max).
So if act 'X' has a minimum fee of 3000 (price_min) and a maximum fee of 6000 (price_max)
Search A: ('X' is in results)
I search with a budget of minimum of 3000 ($min_price) and a maximum of 5500 ($max_price) i get 'X' returned in the results.
Search B: ('X' is not in results)
I search with a budget of minimum of 3500 ($min_price) and a maximum of 5500 ($max_price) i would expect to see 'X' returned in the results as i could still afford him given my budget.
I need 'X' to return in both instances
Could someone help point me in the right direction?
shouldn't it be
array(
'key' => 'price_min',
'value' => $min_price,
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
),
array(
'key' => 'price_max',
'value' => $max_price,
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
If you selected both minimum and maximum price than your query like ::
array(
'key' => 'meta_key_min_price',
'value' => array( $min_p, $max_p ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
array(
'key' => 'meta_key_max_price',
'value' => array( $min_p, $max_p ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
If you select only one of them than your query look like this ::
For Minimum
array(
'key' => 'meta_key_min_price',
'value' => $min_p,
'type' => 'numeric',
'compare' => '>=',
),
For Maximum
array(
'key' => 'meta_key_max_price',
'value' => $min_p,
'type' => 'numeric',
'compare' => '<=',
),
$tax_query = array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $ajs_cat,
)
);
$args = array(
'post_type' => 'post',
'orderby' => 'menu_order',
'posts_per_page' => $per_page,
'paged' => $paged,
'tax_query' => $tax_query,
'meta_query' => array(
array(
'key' => 'price_min',
'value' => $min_price,
'compare' => '>='
),
array(
'key' => 'price_max',
'value' => $max_price,
'compare' => '<='
),
)
);

WP_Query - Using multiple AND / OR

I have the below PHP code:-
$args = array(
'posts_per_page'=> -1,
'post_type' => 'jobs',
'order' => 'ASC',
's' => $search_field,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'job_salary_to',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
)
)
);
This will check to see if the search is between the minimum and maximum values which all works perfectly okay.
Now I want to add a further query which will check if it matches up with a Job Sector, e.g. I added the following below 'meta_query':-
'relation' => 'AND',
array(
'key' => 'job_sector',
'value' => 'Finance',
'compare' => 'LIKE',
),
However, it seems it is just ignoring the above and I'm not sure why. Any help would be much appreciated!
So it looks like this now:-
<?php
$args = array(
'posts_per_page'=> -1,
'post_type' => 'jobs',
'order' => 'ASC',
's' => $search_field,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'job_salary_to',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
),
'relation' => 'AND',
array(
'key' => 'job_type',
'value' => $job_type,
'compare' => 'LIKE',
),
array(
'key' => 'job_location',
'value' => $job_location,
'compare' => 'LIKE',
)
);
$fetch_jobs = new WP_Query( $args );?>
The job_type / job_location conditions should be put under 'meta_query' I guess:
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'job_type',
'value' => $job_type,
'compare' => 'LIKE',
),
array(
'key' => 'job_location',
'value' => $job_location,
'compare' => 'LIKE',
),
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'job_salary_to',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
)
)

WP_Query, using AND with OR

I have the below PHP:-
$args = array(
'posts_per_page'=> -1,
'post_type' => 'jobs',
'order' => 'ASC',
's' => $search_field,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => $job_salary_from,
'compare' => '>=',
),
array(
'key' => 'job_salary_from',
'value' => $job_salary_to,
'compare' => '<=',
),
array(
'relation' => 'OR',
array(
'key' => 'job_salary_to',
'value' => $job_salary_from,
'compare' => '>=',
),
array(
'key' => 'job_salary_to',
'value' => $job_salary_to,
'compare' => '<=',
),
))
);
Lets say there is a job that is from 19000 to 22000 called 'ABC', so
job_salary_from = 19000
and job_salary_to = 22000.
Now lets say I search a job that is between 10000 and 19000, so $job_salary_from = 10000 and $job_salary_to = 19999.
This shows up when I do a search which is correct. However, if I search for a job that is between 20000 and 29999, so $job_salary_from = 19999 and $job_salary_to = 29999 nothing is showing up.
Where as the job 'ABC' should show up because $job_salary_to is within the search bracket.
Any help would be much appreciated.
I think the meta_query syntax is not quite right. Try this:
$args = array(
'posts_per_page'=> -1,
'post_type' => 'jobs',
'order' => 'ASC',
's' => $search_field,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'job_salary_to',
'value' => $job_salary_from,
'type' => 'numeric', //for each case
'compare' => '>=',
),
array(
'key' => 'job_salary_to',
'value' => $job_salary_to,
'type' => 'numeric',
'compare' => '<=',
),
),
array(
'relation' => 'AND',
array(
'key' => 'job_salary_from',
'value' => $job_salary_from,
'type' => 'numeric',
'compare' => '>=',
),
array(
'key' => 'job_salary_from',
'value' => $job_salary_to,
'type' => 'numeric',
'compare' => '<=',
),
)
)
);
Also you can try to use BETWEEN, even I am not sure if this is inclusive or exclusive compare.
that is because there is a different function to fetch results in between a range.
Try the following example to fetch results in between a range
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );
More resources here https://codex.wordpress.org/Class_Reference/WP_Query

Complex meta query not working

I have very complex meta query that breaks into two part. Each part working fine alone but when I compare both part with or it should show me combination of both result(s), but its not showing anything.
First snippet
array(
'relation' => 'AND',
array(
'relation' => 'AND',
array(
'key' => 'range1_min',
'value' => floatval($_GET['m']),
'type' => 'DECIMAL',
'compare' => '<='
),
array(
'key' => 'range1_max',
'value' => floatval($_GET['m']),
'type' => 'DECIMAL',
'compare' => '>='
)
),
array(
'relation' => 'AND',
array(
'key' => 'range1_min',
'value' => floatval($_GET['n']),
'type' => 'DECIMAL',
'compare' => '<='
),
array(
'key' => 'range1_max',
'value' => floatval($_GET['n']),
'type' => 'DECIMAL',
'compare' => '>='
)
)
);
Second snippet
array(
'relation' => 'AND',
array(
'relation' => 'AND',
array(
'key' => 'range2_min',
'value' => floatval($_GET['m']),
'type' => 'DECIMAL',
'compare' => '<='
),
array(
'key' => 'range2_max',
'value' => floatval($_GET['m']),
'type' => 'DECIMAL',
'compare' => '>='
)
),
array(
'relation' => 'AND',
array(
'key' => 'range2_min',
'value' => floatval($_GET['n']),
'type' => 'DECIMAL',
'compare' => '<='
),
array(
'key' => 'range2_max',
'value' => floatval($_GET['n']),
'type' => 'DECIMAL',
'compare' => '>='
)
)
);
Individually they both are working fine, but when I put them together with "OR" condition they are not working.
Full code snippet
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'relation' => 'AND',
array(
'key' => 'range1_min',
'value' => floatval($_GET['m']),
'type' => 'DECIMAL',
'compare' => '<='
),
array(
'key' => 'range1_max',
'value' => floatval($_GET['m']),
'type' => 'DECIMAL',
'compare' => '>='
)
),
array(
'relation' => 'AND',
array(
'key' => 'range1_min',
'value' => floatval($_GET['n']),
'type' => 'DECIMAL',
'compare' => '<='
),
array(
'key' => 'range1_max',
'value' => floatval($_GET['n']),
'type' => 'DECIMAL',
'compare' => '>='
)
)
) ,
array(
'relation' => 'AND',
array(
'relation' => 'AND',
array(
'key' => 'range2_min',
'value' => floatval($_GET['m']),
'type' => 'DECIMAL',
'compare' => '<='
),
array(
'key' => 'range2_max',
'value' => floatval($_GET['m']),
'type' => 'DECIMAL',
'compare' => '>='
)
),
array(
'relation' => 'AND',
array(
'key' => 'range2_min',
'value' => floatval($_GET['n']),
'type' => 'DECIMAL',
'compare' => '<='
),
array(
'key' => 'range2_max',
'value' => floatval($_GET['n']),
'type' => 'DECIMAL',
'compare' => '>='
)
)
)
);
Any help would be greatly appreciated.
When I generate sql query of wordpress and run it on phpmyadmin, it generate error and saying that I have exceeded default join setting, means in my query I have too many join.
I have resolve this error by manually put this sql query before my wordpress query.
global $wpdb
$wpdb->query("SET SQL_BIG_SELECTS = 1");

Wordpress meta_query multiple fields search NOT working

I have this piece of code and it works just fine:
$search = array(
'meta_query' => array(
array(
'key' => 'wpcf-community-city',
'value' => $search_param,
'compare' => 'LIKE'
)
)
);
But when I change to this, it stops working.
$search = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'wpcf-community-city',
'value' => $search_param,
'compare' => 'LIKE'
),
array(
'key' => 'wpcf-community-state',
'value' => $search_param,
'compare' => 'LIKE'
),
array(
'key' => 'wpcf-community-zip',
'value' => $search_param,
'compare' => 'LIKE'
)
)
);
I'm using Wordpress 3.4.2
PS: This piece of code is a part of the query_posts() parameters.
Why u dont use WP_Query ?
U need use the 'relation' parameter ?
Use this example.. its work:
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'type' => 'CHAR',
'compare' => '=' //'=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'
),
array(
'key' => 'price',
'value' => array( 1,200 ),
'compare' => 'NOT LIKE'
)
),

Categories