Mixed OR AND taxonomy query - wordpress

I have created a WP Query argument list like so hoping it will select all posts with term 2 and then from results all others with relation OR.
$similar_properties_args = array(
'post_type' => 'property',
'posts_per_page' => '10',
'post__not_in' => array(get_post_ID() ),
'post_parent__not_in' => array(get_post_ID() ),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'language',
'terms' => '2'
),
array (
'relation' => 'OR',
array(
array(
'taxonomy' => 'property-feature',
'field' => 'id',
'terms' => array(1954,1958,1966,1970,1972)
),
array(
'taxonomy' => 'property-city',
'field' => 'id',
'terms' => array(145)
),
array(
'taxonomy' => 'property-status',
'field' => 'id',
'terms' => array(38)
),
array(
'taxonomy' => 'property-type',
'field' => 'id',
'terms' => array(21)
)
)
)
)
);
The actual WP mysql query created is as as below:
SELECT SQL_CALC_FOUND_ROWS agn_posts.ID FROM agn_posts LEFT JOIN agn_term_relationships ON (agn_posts.ID = agn_term_relationships.object_id) LEFT JOIN agn_term_relationships AS tt1 ON (agn_posts.ID = tt1.object_id) LEFT JOIN agn_term_relationships AS tt2 ON (agn_posts.ID = tt2.object_id) LEFT JOIN agn_term_relationships AS tt3 ON (agn_posts.ID = tt3.object_id) LEFT JOIN agn_term_relationships AS tt4 ON (agn_posts.ID = tt4.object_id) WHERE 1=1 AND agn_posts.ID NOT IN (1440) AND agn_posts.post_parent NOT IN (1440) AND (
agn_term_relationships.term_taxonomy_id IN (2)
AND
(
(
tt1.term_taxonomy_id IN (1954,1958,1966,1970,1972)
AND
tt2.term_taxonomy_id IN (145)
AND
tt3.term_taxonomy_id IN (38)
AND
tt4.term_taxonomy_id IN (21)
)
)
) AND agn_posts.post_type = 'property' AND (agn_posts.post_status = 'publish' OR agn_posts.post_status = 'private') GROUP BY agn_posts.ID ORDER BY agn_posts.post_date DESC LIMIT 0, 10
...and it is not giving me the results as I would need to get.
If I change the tt1 term relationship to OR, then the result is fine.
tt1.term_taxonomy_id IN (1954,1958,1966,1970,1972)
OR //from AND to OR
tt2.term_taxonomy_id IN (145)
OR //from AND to OR
tt3.term_taxonomy_id IN (38)
OR //from AND to OR
tt4.term_taxonomy_id IN (21)
The problem Is, from my naked eye, the argument list above is valid for creating the query I am looking for, but somehow something is missing. May be you can see, where is the problem in my code?

You shouldn't nest the four arrays/clauses in an array:
'tax_query' => array(
'relation' => 'AND', // for clauses 1 and 2
array( // clause 1
'taxonomy' => 'language',
'terms' => '2',
),
array( // clause 2
'relation' => 'OR', // for sub-clause 1..
// you shouldn't nest the sub-sub-clauses
array( // sub-clause 1
array( // sub-sub-clause 1
'taxonomy' => 'property-feature',
...
),
array( // sub-sub-clause 2
'taxonomy' => 'property-city',
...
),
array( // sub-sub-clause 3
'taxonomy' => 'property-status',
...
),
array( // sub-sub-clause 4
'taxonomy' => 'property-type',
...
),
),
),
),
Instead, make the arrays same level as the relation:
'tax_query' => array(
'relation' => 'AND', // for clauses 1 and 2
array( // clause 1
'taxonomy' => 'language',
'terms' => '2',
),
array( // clause 2
'relation' => 'OR', // for the sub-clauses 1, 2, 3 and 4
// instead of nesting, do like so:
array( // sub-clause 1
'taxonomy' => 'property-feature',
'terms' => array( 1954, 1958, 1966, 1970, 1972 ),
),
array( // sub-clause 2
'taxonomy' => 'property-city',
'terms' => array( 145 ),
),
array( // sub-clause 3
'taxonomy' => 'property-status',
'terms' => array( 38 ),
),
array( // sub-clause 4
'taxonomy' => 'property-type',
'terms' => array( 21 ),
),
),
),
And that one would give you this SQL command as part of the WHERE:
agn_term_relationships.term_taxonomy_id IN (2) # language
AND
(
tt1.term_taxonomy_id IN (1954,1958,1966,1970,1972) # feature
OR
tt1.term_taxonomy_id IN (145) # city
OR
tt1.term_taxonomy_id IN (38) # status
OR
tt1.term_taxonomy_id IN (21) # type
)
And BTW, 'field' => 'id' should be 'field' => 'term_id', but the default field is indeed term_id, so you may omit the field arg. Also, WordPress does not have a function named get_post_ID, so I think you meant to use get_the_ID()? :)

Related

Get a order data from Awebsite and create order on website B Woocoomerce API

I want to get product_id (from Website A ) Woocommerce APi and create a order in website B
If from Website A product_id =11 then create product_id= 1050 in website B
If product_id =121 then create product_id= 160 in website B
function create_order_from( $customer_id, $new_customer_data, $password_generated ) {
$live_ck = 'ck_blablabla';
$live_cs = 'cs_blablabla';
$live_url = 'https://www.website.com/wp-json/wc/v3/orders?consumer_key=' . $live_ck . '&consumer_secret=' . $live_cs;
$customer = new WC_Customer( $customer_id );
$body = array(
'status' => 'completed',
'meta_data' => array( array(
'key' => 'createdby',
'value' => 'website'
)),
'total' => 0,
'billing' => array(
'first_name' => $customer->get_billing_first_name(),
'email' => $customer->get_email(),
),
'line_items' => array( array(
'product_id' => 1050,
'quantity' => 1,
)),
);
$raw_response = wp_remote_post( $live_url,
array(
'headers' => array( 'Content-Type' => 'application/json' ),
'timeout' => 30,
'body' => json_encode( $body ),
)
);
}```
THank you for your help

Wordpress : month between 2 dates

I would like to retrieve posts based on the current month.
My customs post type has a start date and an end date.
If the current month is between this period, then retrieve the items.
Someone can help me ?
$date_month = (new DateTime)->format('m');
$start = get_field('start_date');
$end = get_field('start_end');
// parameters of $upcoming
$args_upcoming_period = array(
'posts_per_page' => 10,
'post_type' => array('sports', 'culture'),
'meta_query' => array(
array(
'key' => $date_month,
'compare' => 'BETWEEN',
'value' => array($start, $end),
)
),
);
$upcoming_period = new WP_Query($args_upcoming_period);
you can use this code:
$start_date = "here_value"; //e.g. 2019-01-01
$end_date = "here_value"; //e.g. 2019-02-01
$metaKey = "here_value"; //e.g. date
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'meta_query' => array(
array(
'key' => $metaKey,
'value' => array($start_date, $end_date),
'compare' => 'BETWEEN',
'type' => 'DATE'
),
)
);
$result = new WP_Query($args);
Please note, var start_date & end_date they must be in the sql format (e.g. YYYY-MM-DD).
And meta_key compile with field you want filter. (e.g. date)

WP_Query returns nothing

I'm trying to make a function
that adds a shortcode which replace the shortcode into the list of attached files with pagination.
The problem is, WP_Query() doesn't return anything.
The functions's code is below.
It's in my functions.php, and It will be called in my content-page.php.
$a = shortcode_atts( array( 'number' => '10'), $atts );
//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'attachment',
'post_parent' => get_the_ID(),
'posts_per_page' => $a['number'],
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
$big = 999999999; // need an unlikely integer
$retuen_string .= paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
And print_r($the_query) result is below.
WP_Query Object (
[query_vars] => Array (
[post_type] => attachment
[post_parent] => 26
[posts_per_page] => 15
[paged] => 1
[error] =>
[m] =>
[p] => 0
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[menu_order] =>
[category__in] => Array ( )
[category__not_in] => Array ( )
[category__and] => Array ( )
[post__in] => Array ( )
[post__not_in] => Array ( )
[tag__in] => Array ( )
[tag__not_in] => Array ( )
[tag__and] => Array ( )
[tag_slug__in] => Array ( )
[tag_slug__and] => Array ( )
[post_parent__in] => Array ( )
[post_parent__not_in] => Array ( )
[author__in] => Array ( )
[author__not_in] => Array ( )
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
)
[tax_query] => WP_Tax_Query Object (
[queries] => Array ( )
[relation] => AND
)
[meta_query] => WP_Meta_Query Object (
[queries] => Array ( )
[relation] =>
)
[date_query] =>
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] => [is_preview] =>
[is_page] =>
[is_archive] =>
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] => 1
[is_404] =>
[is_comments_popup] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash] => ab31e2fb8fd323e014706374fb98b349
[query_vars_changed] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[query] => Array (
[post_type] => attachment
[post_parent] => 26
[posts_per_page] => 15
[paged] => 1
)
[request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_parent = 26 AND wp_posts.post_type = 'attachment' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 15
[posts] => Array ( )
)
Can anyone help, please?
$a = shortcode_atts( array( 'number' => '10'), $atts );
//Protect against arbitrary paged values
$temp = $the_query;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'attachment',
'post_parent' => get_the_ID(),
'posts_per_page'=>'15',
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
$big = 999999999; // need an unlikely integer
$retuen_string .= paginate_links( array(
'base' => #add_query_arg('paged','%#%'),
'format' => '?paged=%#%',
'current' => $paged,
'total' => $the_query->max_num_pages
));
wp_reset_postdata();
$the_query = null;
$the_query = $temp;
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
I found the solution.
Had to add 'post_status' => 'inherit' in the $args.
Like this.
$args = array(
'post_type' => 'attachment',
'post_parent' => get_the_ID(),
'posts_per_page'=>'15',
'paged' => $paged,
'post_status' => 'inherit'
);
OMG. so simple.
The code was nothing wrong.
I just passed the wrong arguments...

Calculating percentage from meta key values of multiple wordpress posts between 2 dates

I have 100 posts all have a meta_key named "isactive" whose value will either be "1" or "0"
I need to calculate the percentage of number of active posts(from 'isactive' metakey value) between any 2 dates
$args0 = array(
'date_query' => array(
array(
'after' => 'January 1st, 2013',
'before' => array(
'year' => 2013,
'month' => 2,
'day' => 28,
),
'inclusive' => true,
),
'meta_key' => 'isactive',
'meta_value' => 0
)
);
$count0 = new WP_Query( $args0 );
$args1 = array(
'date_query' => array(
array(
'after' => 'January 1st, 2013',
'before' => array(
'year' => 2013,
'month' => 2,
'day' => 28,
),
'inclusive' => true,
),
'meta_key' => 'isactive',
'meta_value' => 1
)
);
$count1 = new WP_Query( $args1 );
echo $percent = $count0/$count1;
Whether this type of query works ?
Else is there any better way to calculate percentage based on the meta key values between 2 dates?

How to get posts by compare 2 custom fields value?

I have "product" custom post type. It have 2 custom fields: price1, price2 (price of product)
I want to get all posts have price1 < price2 (compare 2 values)
How can i do that?
Here's a nice sample that should help you out:
$args = array(
'tax_query' => array(
'taxonomy' => 'custom_taxonomy_name',
array(
'key' => 'price',
'value' => array( 100, 200 ),
'compare' => 'BETWEEN',
'type' => 'numeric',
),
array(
'key' => 'description',
'value' => 'round',
'compare' => 'NOT LIKE'
)
)
);
$query = new WP_Query($args);

Resources