Wordpress meta_query search by area (lat, lng) - wordpress

I have area (lat0, lat1, lng0, lng1). Why my wp_query not working?
$new_query->query( array(
'post_type' => 'object',
'meta_query' => array(
array(
'relation' => 'AND',
array(
'key' => 'lat',
'compare' => 'BETWEEN',
'value' => array($lat0, $lat1),
'type' => 'DECIMAL',
),
array(
'key' => 'lng',
'compare' => 'BETWEEN',
'value' => array($lng0, $lng1),
'type' => 'DECIMAL',
)
),
),
)
);
I get some result if I use:
array(
'key' => 'lat',
'compare' => '<=',
'value' => $lat0,
'type' => 'DECIMAL',
),
But this code not working:
array(
'relation' => 'AND',
array(
'key' => 'lat',
'compare' => '>=',
'value' => $lat0,
'type' => 'DECIMAL',
),
array(
'key' => 'lat',
'compare' => '<=',
'value' => $lat1,
'type' => 'DECIMAL',
)
),
Can I use wp_query for search by area. And if this wrong way, what I can do?

Fixed
global $wpdb;
$querystr = "
SELECT *
FROM $wpdb->posts
INNER JOIN $wpdb->postmeta m1
ON ( $wpdb->posts.ID = m1.post_id )
INNER JOIN $wpdb->postmeta m2
ON ( $wpdb->posts.ID = m2.post_id )
INNER JOIN $wpdb->postmeta m3
ON ( $wpdb->posts.ID = m3.post_id )
WHERE
$wpdb->posts.post_type = 'object'
AND $wpdb->posts.post_status = 'publish'
AND ( m1.meta_key = 'lat' AND m1.meta_value < $lat0 )
AND ( m1.meta_key = 'lat' AND m1.meta_value > $lat1 )
AND ( m2.meta_key = 'lng' AND m2.meta_value < $lng0 )
AND ( m2.meta_key = 'lng' AND m2.meta_value > $lng1 )
AND ( m3.meta_key = 'city' AND m3.meta_value = $city_string )
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.post_date
DESC;
";
$pageposts = $wpdb->get_results($querystr, OBJECT);

Related

i want to execute argument in loop

if(get_post_meta(get_the_ID(),$field,true) && $field == 'is_this_a_job' )
{
$today = date('Ymd');
$args = array(
'meta_query' => array(
array(
'meta_key' => 'last_date',
'orderby' => 'meta_value_num',
)
)
);

How can I get all today's order in WooCommerce? [duplicate]

This question already has answers here:
Get orders total purchases amount for the day in Woocommerce
(2 answers)
Closed 4 years ago.
I am making a wordpress plugin.
I want to fetch all the woo commerce order data into the plugin. How can I do?
This is not proper way that I am using.
$order = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
) );
for($o=0;$o<count($order);$o++):
$order_details = get_post_meta( $order[$o]->ID );
$customer_name = $order_details[_billing_first_name][0].' '.$order_details[_billing_last_name][0];
$customer_phone = $order_details[_billing_phone][0];
$customer_email = $order_details[_billing_email][0];
$customer_city = $order_details[_billing_city][0];
$customer_state = $order_details[_billing_state][0];
$customer_state = $order_details[_billing_state][0];
}
$query_args = array(
'fields' => 'ids',
'post_type' => 'shop_order',
'post_status' => array_keys( wc_get_order_statuses() ),
'posts_per_page' => -1,
//'numberposts' => -1,
'date_query' => array(
array(
'before' => $end_date, // replace desired date
'after' => $start_date, // replace desired date
'inclusive' => true,
),
),
);
$query = new WP_Query($query_args);

Wordpress meta_query filtering type numeric is not working

I have a meta_query like this:
$meta_query_args = array(
'relation' => 'AND',
array(
'key' => 'apartment-price',
'value' => array(50000, 100000),
'compare' => 'BETWEEN',
'type' => 'numeric'
),
array(
'key' => 'apartment-block',
'value' => $data['block'],
'compare' => 'IN'
),
array(
'key' => 'apartment-floor',
'value' => intval($data['floor']),
'compare' => 'IN',
'type' => 'numeric'
)
);
$meta_query = new WP_Query(
array(
'post_type' => 'apartment',
'meta_query' => $meta_query_args
)
);
And I want to filter apartment-price between 50000 and 100000, but I get query like this:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) WHERE 1=1 AND (
( wp_postmeta.meta_key = 'apartment-price' AND CAST(wp_postmeta.meta_value AS SIGNED) BETWEEN '50000' AND '100000' )
AND
( mt1.meta_key = 'apartment-block' AND mt1.meta_value IN ('B') )
AND
( mt2.meta_key = 'apartment-floor' AND CAST(mt2.meta_value AS SIGNED) IN ('1') )
) AND wp_posts.post_type = 'apartment' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
So it doesn't work because the numbers have been quoted. Why parameter 'type' is not working? Help please!
BTW apartment-floor filter is not working too, same reason.

Wordpress custom taxonomy category filter, default value to get all categories

I am trying create filter with AJAX which filter different taxonomy categories. For each taxonomy is filter listbox.
Filter is working if all listbox are chosen, but i need find way how filter categories if some of filter are not selected. How can query categories with empty value ? I have 4 listboxes, code is shortened.
Code:
$komanda = $_POST['komanda'];
$pretinieks = $_POST['pretinieks'];
if ($komanda == 0){ $komanda = '?'; }
if ($pretinieks == 0){ $pretinieks = '?'; }
$args = array(
'post_type' => 'speles',
'posts_per_page' => 99,
'post_status' => array('publish'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'komandas',
'field' => 'term_id',
'terms' => $komanda,
),
array(
'taxonomy' => 'pretinieki',
'field' => 'term_id',
'terms' => $pretinieks,
),
)
);
I found solution.
$komanda = $_POST['komanda'];
$pretinieks = $_POST['pretinieks'];
if ($komanda == 0){
$terms = get_terms( 'komandas', 'orderby=count&hide_empty=1' );
$komanda = array();
$komanda = wp_list_pluck($terms, 'term_id');
}
if ($pretinieks == 0){
$terms = get_terms( 'pretinieki', 'orderby=count&hide_empty=1' );
$pretinieks = array();
$pretinieks = wp_list_pluck($terms, 'term_id');
}
$args = array(
'post_type' => 'speles',
'posts_per_page' => 99,
'post_status' => array('publish'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'komandas',
'field' => 'term_id',
'terms' => $komanda,
),
array(
'taxonomy' => 'pretinieki',
'field' => 'term_id',
'terms' => $pretinieks,
),
)
);

get minimum price variation and maximum price variation according to specific category woocommerce

I want to get minimum price and maximum price based on categories.
The woocommerce query which gives me minimum and maximum product price range but i want it on the bases of category.
for example: category="music,clothing".
Here is query for minimum and maximum price:
$min = floor( $wpdb->get_var(
$wpdb->prepare('
SELECT min(meta_value + 0)
FROM %1$s
LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price', '_min_variation_price' ) ) ) . '")
AND meta_value != ""
', $wpdb->posts, $wpdb->postmeta )
) );
$max = ceil( $wpdb->get_var(
$wpdb->prepare('
SELECT max(meta_value + 0)
FROM %1$s
LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
WHERE meta_key IN ("' . implode( '","', apply_filters( 'woocommerce_price_filter_meta_keys', array( '_price' ) ) ) . '")
', $wpdb->posts, $wpdb->postmeta, '_price' )
) );
Please suggest me how can i get it according to category selected.
SQL Query:
//SQL Query to get max price :
SELECT max(meta_value + 0) FROM wp_posts LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE meta_key = '_price' AND (wp_term_relationships.term_taxonomy_id IN (46,47));
//SQL Query to get min price :
SELECT min(meta_value + 0) FROM wp_posts LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE (meta_key = '_price' OR meta_key='_min_variation_price') AND (wp_term_relationships.term_taxonomy_id IN (46,47));
//46, 47 is the term id of category. So you need to have id of music and clothing
WP_Query :
This is the way how I do it :
<?php
$category = array('t-shirt');
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category,
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => '_price',
)
)
);
$loop = new WP_Query($args);
echo "Max :" get_post_meta($loop->posts[0]->ID, '_price', true);
?>
I used 'order'=>'DESC' so that it will sort by Highest to Lower and hence we can get Highest from it.
If you want to have Min, change 'order'=>'DESC' to 'order'=>'ASC' and you will have echo "Min :" get_post_meta($loop->posts[0]->ID, '_price', true);
Rohil_PHPBeginner's answer is correct, but you do no need to get prices for all products to find the maximum. You can just alter the "posts_per_page" to be 1:
<?php
$category = array('t-shirt');
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category,
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => '_price',
)
)
);
$loop = new WP_Query($args);
echo "Max :" get_post_meta($loop->posts[0]->ID, '_price', true);
wp_reset_postdata();
?>

Resources