WP_Query based on meta data boolean - php

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 );

Related

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' );

WordPress WP_User_Query

How can I show user and order them by 2 meta fields? For example I have user query
$args = array(
'role' => 'specialist',
'meta_key' => 'proffession',
'meta_value' => $term->ID,
'meta_compare' => '=',
'number' => 20,
'paged' => $paged,
);
$user_query = new WP_User_Query($args);
I want to sort this query by two meta_values, for example in PHP:
if(get_usermeta( $user_id, $meta_key = 'be_first' )>time() AND get_usermeta( $user_id, $meta_key = 'pro_date' )>time())
// show this users first ordered by be_first DESC, ordered by pro_date
if($pro_date>time()){
//show this users second ordered by pro_date desc
}
//show all other users
You can try this... I don't have your fields, so this is not tested. You can create a meta_query where your clauses are set to compare exists then use those to orderby.
$args = array(
'role' => 'specialist',
'number' => 20,
'paged' => $paged,
'meta_query' => array (
'relation' => 'AND',
array(
'key' => 'profession',
'value' => '$term->ID',
'compare' => '=',
),
'be_first_clause' => array(
'key' => 'be_first',
'compare' => 'EXISTS',
),
'pro_date_clause' => array(
'key' => 'pro_date',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'be_first_clause' => 'ASC',
'pro_date_clause' => 'ASC'
)
);
$user_query = new WP_User_Query($args);

Wordpress query for Custom Field number BETWEEN two query variables

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'

not getting any result by meta field in custom form

I have a search form where my form action is this:
<form action="<?php echo esc_url(home_url('/searched-result')); ?>" method="post">
On the searched-result template I have this query,
<?php
// Only check these form fields (change the list as needed...)
$fields = array( 'custom_price', 'custom_beds', 'custom_garage');
foreach( $fields as $field ) {
if( $_REQUEST[$field] != '' ) {
// We have something to match, otherwise ignore the field...
$meta_query[] = array(
'key' => $field,
'value' => $_REQUEST[$field], // This is OK, WP_Query will sanitize input!
'compare' => 'LIKE'
);
}
}
$args = array(
'post_type' => 'property_post',
'posts_per_page' => 1000,
'meta_key' => 'SORTFIELD', // The name of the metakey to orderby
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => $meta_query,
);
$query = new WP_Query( $args );
if (have_posts()) : while (have_posts()) : the_post();
?>
<h1 class="title">
<?php the_title(); ?></h1>
The way you have it will only return results if all meta_values match the criteria. You must add an extra argument called relation, and set it as 'OR': Example:
$args = array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );
Not tested :
$fields = array( 'custom_price', 'custom_beds', 'custom_garage');
foreach( $fields as $field ) {
if( isset($_REQUEST[$field]) && !empty($_REQUEST[$field]) ) {
// We have something to match, otherwise ignore the field...
$meta_query[] = array(
'key' => $field,
'value' => $_REQUEST[$field], // This is OK, WP_Query will sanitize input!
'compare' => 'LIKE'
);
}
}
Check if your getting value in $_REQUEST.
Hope this helps...
$args = array(
'post_type' => 'property_post',
'posts_per_page' => 1000,
'meta_key' => 'SORTFIELD', // The name of the metakey to orderby
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
$meta_query
)
);

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