WP_Query - Load delay with multiple meta_query - wordpress

I have a Wordpress site that uses quite a few filters to display jobs.
The query will filter jobs by:-
The Job Type
The Location
Job Sector
If it is an International Job
Will check the salary between two values
The current query I am running is:-
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page'=> -1,
'post_type' => 'jobs',
'order' => 'DESC',
'posts_per_page' => 20,
'paged' => $paged,
's' => $search_field,
'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(
'key' => 'job_sector',
'value' => $job_sector,
'compare' => 'LIKE',
),
array(
'key' => 'international_job',
'value' => $international_job,
'type' => 'numeric',
'compare' => 'IN',
),
array(
'key' => 'job_location',
'value' => $location_array,
'compare' => 'NOT IN',
),
),
array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'anual_to_salary',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'AND',
array(
'key' => 'anual_from_salary',
'value' => array($job_salary_from,$job_salary_to),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
array(
'relation' => 'OR',
array(
'key' => 'job_negotiable',
'value' => 'yes',
'compare' => '=',
),
),
),
)
);
$fetch_jobs = new WP_Query( $args );?>
I'm assuming this is a bad way to do this kind of query as it's taking around 5 - 10 seconds to display any results.
Can anyone recommend a better way to do this type of query with multiple filters?
Thanks in advance.

First of all Your code is like a charm, there is no better way to execute wp_query();. it is taking 5 to 10 second because there is lot of conditions to check and it totally depends on the data on which it have to perform filtration.
I saw whole code and just need a little change that you pass same argument twice i.e. posts_per_page please correct it. else it is the best to perform wp_query.

Related

Sort WP_Query using multiple meta_keys not working

I have a custom post type, that has price in 2 different meta keys, based on product type. I want to filter the query so that all products can be displayed in either asc or desc order. Here is my query and it is not sorting the results.
$args = array(
'post_type' => 'ars-products',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'ars_product_price',
'compare' => 'EXISTS',
),
array(
'key' => 'ars_product_min_amount',
'compare' => 'EXISTS',
)
),
array(
'relation' => 'OR',
array(
'key' => 'ars_product_type',
'value' => 'one_time',
'compare' => '=',
),
array(
'key' => 'ars_product_type',
'value' => 'donation',
'compare' => '=',
),
),
),
'orderby' => array(
'ars_product_price' => 'ASC',
'ars_product_min_amount' => 'ASC'
)
);
$products_data = new \WP_Query( $filters );
Edit: it does sort perfectly, if I only use one meta key ars_product_price.

Wordpress Custom Post type and post type child filter

i want to make a filter, where i can list employees
however the employees are from post type employees (example) and their contracts are made in child post type called contracts ( example ) so i have made this code but i can't take all info that i need
$args = array( 'post_type' => array( 'employees', 'contracts'),
'posts_per_page' => -1,
'paged' => $paged,
'orderby'=>'title',
'order'=> $order,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'wpcf-podruznici',
'value' => $podruznici,
'type' => 'CHAR',
'compare' => '='
),
array(
'key' => 'wpcf-direkcija_podruznica',
'value' => $podruznici,
'type' => 'CHAR',
'compare' => '='
),
array(
'key' => 'wpcf-opredeneno_neopredeleno_vraboten',
'value' => $dogovor,
'type' => 'CHAR',
'compare' => '='
)
)
this array here
array(
'key' => 'wpcf-opredeneno_neopredeleno_vraboten',
'value' => $dogovor,
'type' => 'CHAR',
'compare' => '='
)
is from the post type contracts
so if in the filter i choose full time or part time i can only list the employees name and it does not give me the other data..
i hope it makes sense

Wordpress Query, check a value against multiple keys

My Wordpress setup has a Publication custom post type and has 10 custom fields each corresponding to an author who may have contributed to that publication. Every post can have different authors in those fields or the same authors but in a different order. I am using Wordpress's WP_Query to search for posts matching my criteria. I need to check a single author name against all the custom fields to see if it matches any one of them. I tried doing this with the below code but it produces no result. Any help would be appreciated!
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year_published',
'value' => $selected_pub_year,
'compare' => '=',
),
array(
'key' => array(
'author_0_name',
'author_1_name',
'author_2_name',
'author_3_name',
'author_4_name',
'author_5_name',
'author_6_name',
'author_7_name',
'author_8_name',
'author_9_name',
),
'value' => $selected_pub_author,
'compare' => '=',
),
),
);
// The above meta-query may be modified through the use of this:
// echo "<pre>".print_r($args[meta_query][relation])."</pre>";
// The Query
$the_query = new WP_Query($args);
I have added OR relation for post author query,Try to change your argument array as follow,
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year_published',
'value' => $selected_pub_year,
'compare' => '=',
),
array(
'relation' => 'OR',
array(
'key' => 'author_0_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_1_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_2_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_3_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_4_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_5_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_6_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_7_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_8_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_9_name',
'value' => $selected_pub_author,
'compare' => '=',
),
),
),
);
$the_query = new WP_Query( $args );

Meta Queries - should nesting work after WP 4.1?

I have a rather huge meta_query here and it hangs at execution: browser just won't load the page, it goes on loading ...
As far as I know this should work after WP 4.1 (I have it). Also tried updating to 4.2 to no avail.
Am I doing something terribly wrong here? Thanks for any info.
$meta_query = array(
'relation' => 'AND',
// IF $level GIVEN
array(
'relation' => 'OR',
array(
'key' => 'co_level',
'value' => $level,
'compare' => 'IN'
),
array(
'relation' => 'AND',
array(
'key' => 'co_program2',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_level2',
'value' => $level,
'compare' => 'IN'
),
),
array(
'relation' => 'AND',
array(
'key' => 'co_program3',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_level3',
'value' => $level,
'compare' => 'IN'
),
),
),
// IF $year GIVEN
array(
'relation' => 'OR',
array(
'key' => 'co_year',
'value' => $year,
'compare' => 'IN'
),
array(
'relation' => 'AND',
array(
'key' => 'co_program2',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_year2',
'value' => $year,
'compare' => 'IN'
),
),
array(
'relation' => 'AND',
array(
'key' => 'co_program3',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_year3',
'value' => $year,
'compare' => 'IN'
),
),
),
);
$args = array(
'post_type' => 'course',
'posts_per_page' => -1, // get all posts
'orderby' => array('meta_value_num'=>'ASC','menu_order'=>'ASC'), // order by given meta value AND menu_order
'meta_key' => $meta_key_orderby,
'order' => 'ASC',
'meta_query' => $meta_query,
);

Wordpress meta_query to compare 3 meta value with same meta key

I would like to fetch all post that have meta_value Book, Study, Art. But when I put 3 array on meta_query system doesn't return anything, but if I put online 2 array It's work
// this working
$args = array(
'numberposts' => 10,
'post_type' => 'content',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'media_type',
'value' => 'Book',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Study',
'compare' => 'LIKE'
)
)
);
//this now working
$args = array(
'numberposts' => 10,
'post_type' => 'content',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'media_type',
'value' => 'Book',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Study',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Art',
'compare' => 'LIKE'
)
)
);
It sounds like you are searching for the exact meta values, so did you try:
$args = array(
'posts_per_page' => 10,
'post_type' => 'content',
'meta_query' => array(
array(
'key' => 'media_type',
'value' => array( 'Art', 'Book','Study' ),
'compare' => 'IN'
),
),
);
as your query arguments?
Sidenote: The parameter numberposts works, but posts_per_page is now more commonly used in WP_Query().

Resources