Wordpress query for Custom Field number BETWEEN two query variables - php

I'm trying to list an archive of queried custom post types named 'jobs', each with a custom field of 'minimum_salary' and 'maximum_salary'.
On the previous search page the user enters two values in the search ('min-salary' & 'max-salary'), which are added to the URL of the archive page.
I want to be able to display ALL jobs that have a 'minimum_salary' AND 'maximum_salary' BETWEEN the min and max salary var the user entered on the previous page.
See below for my code so far, thanks for the help.
<?php
$minSalaryVar = get_query_var('min-salary');
$maxSalaryVar = get_query_var('max-salary');
$taxArgs = array();
if ($minSalaryVar && $maxSalaryVar) {
$taxArgs[] = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'minimum_salary',
'value' => array($minSalaryVar, $maxSalaryVar),
'compare' => 'BETWEEN',
'type' => 'numeric'
),
array(
'key' => 'maximum_salary',
'value' => array($minSalaryVar, $maxSalaryVar),
'compare' => 'BETWEEN',
'type' => 'numeric'
),
)
);
}
if ($taxArgs) {
$args = array (
'tax_query' => $taxArgs,
);
$argsNew = array_merge( $wp_query->query_vars, $args );
query_posts( $argsNew );
} ?>

You are using 'compare' = 'BETWEEN' two times when you need either using it just once or compare twice one greater than minimum salary and the other bigger. Following I'll let both options:
1:
if ($minSalaryVar && $maxSalaryVar) {
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'minimum_salary',
'value' => $minSalaryVar,
'compare' => '>=',
'type' => 'numeric'
),
array(
'key' => 'maximum_salary',
'value' => $maxSalaryVar,
'compare' => '<=',
'type' => 'numeric'
),
);
}
if ($taxArgs) {
$args = array (
'meta_query' => $meta_query,
);
$argsNew = array_merge( $wp_query->query_vars, $args );
query_posts( $argsNew );
} ?>
2:
$taxArgs = array();
if ($minSalaryVar && $maxSalaryVar) {
$meta_query = array(
'key' => 'minimum_salary',
'value' => array($minSalaryVar,$maxSalaryVar),
'compare' => 'BETWEEN
'type' => 'numeric'
);
}
if ($taxArgs) {
$args = array (
'meta_query' => $meta_query,
);
$argsNew = array_merge( $wp_query->query_vars, $args );
query_posts( $argsNew );
} ?>
--- EDIT
You should use 'meta_query' instead of 'tax_query'

Related

How to create a dynamic WP query using nested if / foreach statements

I am trying to create a dynamic WP query based on custom fields in a page but cannot work out how to do it without syntax errors.
Can anybody advise on the approach to take to achieve this sort of thing, am I getting it completely wrong?
See below my code and an example of the desired query to be built from the custom fields...
Here is my code:
if( have_rows('query') ):
while( have_rows('query') ): the_row();
$query_name = get_sub_field('query_name');
$queries= get_sub_field ('queries');
if( $queries ) {
foreach( $queries as $query ) {
$created_queries= $query['create_query'];
if( $created_queries ) {
foreach( $created_queries as $created_query ) {
$type= $created_query['statement_type'];
$rules= $created_query['rules'];
if( $rules ) {
foreach( $rules as $rule ) {
$key= $rule['key'];
$operator= $rule['operator'];
$value=$rule['value'];
}
}
}
$query_name = array(
'post_type' => 'candidates',
'meta_query' => array(
'relation' => $type ,
array(
'key' => $key,
'value' => $value,
'compare' => $operator,
),
),
);
$test = new WP_Query( $query_name );
echo $query_name, $test-post_count;
}
}
}
endwhile;
endif;
Example of desired output:
$args_red = array(
'post_type' => 'candidates',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'available_for_current_start_date',
'value' => 'N',
'compare' => '=',
),
array(
'key' => 'start_date_difference',
'value' => '7',
'compare' => '<='
),
'relation'=> 'AND',
array(
'key' => 'forename',
'value' => 'test',
'compare' => '=',
),
),
);
$red = new WP_Query( $args_red );

Woocommerce - Hiding "SPECIALS" row if no products are on sale

I have a site that has an Owl Carousel row dedicated to the Weekly Specials.
I am trying to hide this row if no products are listed as "On Sale". This is what I have so far, but something is not working and I can't figure out what. All help appreciated.
// -----------------------------------------
// HIDE WEEKLY SPECIALS IF THERE ARE NO PRODUCTS
function weeklyspecials() {
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$loop = new WP_Query( $args );
if ( ! $loop->have_posts() ) {
echo '<style>#weeklyspecials {display:none;}</style>';
}
wp_reset_postdata();
}
add_action ( 'wp_body_open', 'weeklyspecials' );
EDIT *** PROBLEM SOLVED
Turned out my issue lied in the fact that there were hidden ON SALE products... why? Because they were out of stock. I had to alter the array with an AND relation related to the stock status to ensure it covered everything.
WORKING CODE:
// HIDE WEEKLY SPECIALS IF THERE ARE NO PRODUCTS
function weeklyspecials() {
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)),
array( // Stock status
'key' => '_stock_status',
'value' => 'instock'
)
)
);
$loop = new WP_Query( $args );
if ( ! $loop->have_posts() ) {
echo '<style>#weeklyspecials {display:none;}</style>';
}
}
add_action ( 'wp_body_open', 'weeklyspecials' );
Thanks for all the help!
Add an else to your if.
// -----------------------------------------
// HIDE WEEKLY SPECIALS IF THERE ARE NO PRODUCTS
function weeklyspecials() {
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$loop = new WP_Query( $args );
if ( ! $loop->have_posts() ) {
echo '<style>#weeklyspecials {display:none;}</style>';
} else {
echo '<style>#weeklyspecials</style>';
}
wp_reset_postdata();
}
add_action ( 'wp_body_open', 'weeklyspecials' );

WP_Query based on meta data boolean

In my wordpress site I have created a meta box for any page which is using post.php
This meta box includes a boolean checkbox where I want to decide whether or not it's going to be included with a WP_Query.
When _is_private page is true I wan't to remove the post from search
Only problem is I can't get the expected result.
See below for my code.
$meta_query = array(
'key' => '_is_private_page',
'value' => 'true',
'compare' => 'NOT EXISTS',
);
$wp_query_args = array(
's' => $_GET['s'],
'meta_query' => $meta_query
);
$query = new WP_Query( $wp_query_args );
You need to select all posts/pages that not private, so they will have meta value not equal true
$meta_query = array(
'key' => '_is_private_page',
'value' => 'true',
'compare' => '!=',
);
$wp_query_args = array(
's' => $_GET['s'],
'meta_query' => $meta_query
);
$query = new WP_Query( $wp_query_args );
To select all that have meta value not equal to true or have no meta:
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_is_private_page',
'compare' => 'NOT EXISTS',
),
array(
'key' => '_is_private_page',
'value' => 'true',
'compare' => '!=',
),
);
$wp_query_args = array(
's' => $_GET['s'],
'meta_query' => $meta_query
);
$query = new WP_Query( $wp_query_args );

How to only apply a (Wordpress) meta query if it exists

I am trying to apply filters in a list of products. Users will use select boxes on the front end and click a button to filter the products.
Every product is it's own post, so I use WP_Query to get the posts. For example, I want all products with the color "red", the material "plastic" and the brand "Bikon". So I use;
$color = "red";
$material = "plastic";
$brand = "Bikon";
$query = new WP_Query(array(
'post_type' => 'products',
'posts_per_page' => 6,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => $color,
'compare' => '='
),
array(
'key' => 'material',
'value' => $material,
'compare' => '='
),
array(
'key' => 'brand',
'value' => $brand,
'compare' => '='
)
)
));
And this will work fine if every value get's set. But if only one of the select boxes is used, the other two values will be empty and the query will not return any posts. Is there any way to say "only use this array in the meta query if this value is set"?
You can check for definition of variable and add additional filtering if it exists
$query_args = array(
'post_type' => 'products',
'posts_per_page' => 6,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'product_check',
'compare' => 'EXISTS',
),
),
);
if(isset($color) && $color) {
$query_args['meta_query'][] = array('key' => 'color', 'value' => $color, 'compare' => '=');
}
if(isset($material ) && $material ) {
$query_args['meta_query'][] = array('key' => 'material ', 'value' => $material , 'compare' => '=');
}
if(isset($brand ) && $brand ) {
$query_args['meta_query'][] = array('key' => 'brand ', 'value' => $brand , 'compare' => '=');
}
$query = new WP_Query($query_args);

Query Wordpress Posts for multiple meta keys with multiple values

Im having trouble with my WordPress query on a project I do for a customer of us. Basically my problem is that I want to grab posts from the database with specified meta_keys. I want to get my posts (wpcompare) with the meta key '_price' and value between xx and yy. Works fine so far. Now I want to add filters for manufacturer, categories and tags. These filter values are all multiple, so that you can select multiple manufacturers. For example Canon and Nikon. So with the WP_MetaQuery I can filter multiple meta_keys and values, but I cant combine the queries with AND or OR.
Let me give you an example, how my query should work:
"Give me all the posts with post_type "wpcompare" where the meta_value _price is between 100 and 200 bucks, and where the manufacturer (_hersteller) is Canon OR Nikon OR Sony".
My head turns crazy, so please help me.
Thank you very much in advance :-)
Here is my Code:
if(isset($_POST) AND !empty($_POST))
{
$meta_query = array(
'relation' => 'AND',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
);
}
else
{
$meta_query = '';
}
$args = array(
'post_type' => 'wpcompare',
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => $meta_query,
'posts_per_page' => ($per_page == false) ? 18 : $per_page,
'ignore_sticky_posts'=> true
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args)
$args = array(
'post_type' => 'posttypehere',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'somekey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'anotherkey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ($query->have_posts()) : $query->the_post();
echo $post_id = get_the_ID();
endwhile;
endif;
$meta_query = array(
'relation' => 'OR',
array(
'key' => '_price',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'somekey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
array(
'key' => 'anotherkey',
'value' => array($_POST['p_from'], $_POST['p_to']),
'type' => 'CHAR',
'compare' => 'BETWEEN'
),
);

Categories