not getting any result by meta field in custom form - php

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

Related

How to get WooComrnce product category by meta key value

I have product categories with the meta value "old_id", I'm trying to make a function that will return the new product category id using the old id (with meta key "old_id").
this is what i have tried :
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false, // also retrieve terms which are not used yet
'meta_query' => array(
array(
'key' => 'old_id',
'value' => $old_cat_id,
'compare' => '='
)
),
)
);
using get_categories does not work for woocomrnce categories.
then i tried another way :
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'meta_query' => array(
array(
'key' => 'old_id',
'value' => $old_cat_id,
'compare' => '='
)
),
),
),
);
$query = new WP_Query($args);
for some reason this query does not work at all, did i miss anything?
add_action('init', 'get_woocommerce_product_cats');
function get_woocommerce_product_cats() {
$orderby = 'name';
$order = 'asc';
$hide_empty = false;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'taxonomy' => 'product_cat',
'meta_key' => 'old_id',
'meta_value' => $old_cat_id
);
$product_categories = get_terms($cat_args);
echo '<pre>';
print_r($product_categories);
echo '</pre>';
exit;
}

Get last Wordpress Post displayed on homepage?

I have theme that use function to get latest posts showed on homepage. But for some reason , it showing previous post , instead latest one. This is function code frorm theme:
$number_of_news = 3;
if( !empty( $theme_options['home_news_count'] ) ) {
$number_of_news = intval( $theme_options['home_news_count'] );
}
$args = array(
'post_type' => 'post',
'posts_per_page' => $number_of_news,
'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'orderby' => 'post_date',
'field' => 'slug',
'terms' => array('post-format-quote', 'post-format-link', 'post-format-audio'),
'operator' => 'NOT IN'
)
),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
),
array(
'key' => 'MEDICAL_META_embed_code',
'compare' => 'EXISTS'
),
array(
'key' => 'MEDICAL_META_gallery',
'compare' => 'EXISTS'
)
)
);
// The Query
$blog_query = new WP_Query($args);
// The Loop
if ($blog_query->have_posts()) {
?>
<div class="row">
<?php
while ($blog_query->have_posts()) {
$blog_query->the_post();
global $post;
$format = get_post_format($post->ID);
if (false === $format) {
$format = 'standard';
}
?>
i searched on google to find how to get latest posted blog post and found this but now sure what to modify on my code to show me last one. Any help?

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'

WP query and multiple key

I m trying to implement a multiple meta key filter in my wordpress. That's simple I get value to a form to filter my post. If I implement this with only "price" query wordked perfectly. If I add "genre" nothing work, query not working.
For field "genre" I m using checkbox from Advanced Custom Fields with this structure "homme : Homme / femme : Femme".
I test different thing like delete "price" and query on "genre" not working too...
I get value from this
<?php
if($_GET['minprice'] && !empty($_GET['minprice']))
{
$minprice = $_GET['minprice'];
} else {
$minprice = 0;
}
if($_GET['maxprice'] && !empty($_GET['maxprice']))
{
$maxprice = $_GET['maxprice'];
} else {
$maxprice = 1000;
}
if($_GET['genre'] && !empty($_GET['genre']))
{
$genre = $_GET['genre'];
}
?>
my query looks like this
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
My loop with my query
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
// create a new instance of WP_Query
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<?php
get_template_part( 'content-category', get_post_format() );
?>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<div class="clearfix"></div>
<?php bootstrap_pagination();?>
<?php } ?>
<?php else: ?>
<?php get_template_part( 'no-results', 'archive' ); ?>
<?php endif; ?>
</div>
<?php wp_reset_query(); ?>
I tested this and it's work !
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
)
);
But that don't work
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
Please, can you help me beacause I m loosing my mind....
Thanks !
I think you are missing an wrapping array in the meta_query
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array( $minprice, $maxprice ),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
),
),
),
);

WordPress Advanced Custom Fields - Meta Query no result

I am using WPML and ACF in my WP.
Now I wanna list posts from the category ID 399 with the ACF Field "organization_type" and the value key "socialbusiness" but they do not show up.
This are my query tries:
$args = array(
'post_type' => 'post',
'cat' => 399,
'posts_per_page' => -1,
'meta_query' => array(
//'relation' => 'OR',
array(
'key' => 'organization_type',
'value' => 'socialbusiness',
//'compare' => '='
)
)
);
//unset($args);
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'cat' => 399,
'meta_key' => 'organization_type',
'meta_value' => 'socialbusiness'
);
// query
query_posts( $args );
while( have_posts() ) {
What am I doing wrong?
You should have just one variable $args because your first declaration of the variable is override by your second variable.
In your case your code should look like :
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => 399
)
),
'meta_query' => array(
array(
'key' => 'organization_type',
'value' => 'socialbusiness',
'compare' => '=',
'type' => 'CHAR'
),
)
);
$items = new WP_Query($args);
?>
<?php if($items->have_posts()) : ?>
<div class='item'>
<?php while($items->have_posts()) : $items->the_post() ?>
.....
<?php endwhile ?>
</div>
<?php endif ?>

Categories