WordPress Loop Not Respecting Post Status - php

I have a series of loops spitting out 'event' custom post types that I want to only pull posts with a post_status of 'publish'. I've added the post_status of 'publish' in my wp_query array but it doesn't seem to work. I still have scheduled posts showing up.
<?php
$args_hotel_feature = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 2,
'meta_key' => '_expiration_date',
'orderby' => 'meta_value',
'order' => 'ASC',
array(
'key' => '_expiration_date',
'value' => date("m-d-Y"),
'compare' => '>=',
'type' => 'NUMERIC,'
),
'tax_query' => array(
array(
'taxonomy' => 'EventCategory',
'terms' => 'hotel-feature',
'field' => 'slug',
)
),
);
$wp_query4 = new WP_Query($args_hotel_feature);
if($wp_query4->have_posts()) :
while($wp_query4->have_posts()) :
$wp_query4->the_post();
?>
Anyone else experienced this issue?

Use meta_query
$args_hotel_feature = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 2,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_expiration_date',
'value' => date("m-d-Y"),
'compare' => '>=',
'type' => 'NUMERIC,'
)
),
'tax_query' => array(
array(
'taxonomy' => 'EventCategory',
'terms' => 'hotel-feature',
'field' => 'slug',
)
),
);

Turns out the query was fine, however, the post expiration plug-in "Posts Expiration Date" breaks post_staus. Use "Post Expirator" instead.

Related

Issue with Woocommerce query by meta and attributes

I have 3 products having meta key 'check_key' = 1. 2 of them have attribute 'pa_size_10_20' with values '100' and '30'; other one have attribute 'pa_size_30_40' with value '70'.
Please help me understand why the query below returns all 3 products? I expect only 2 products with 'pa_size_10_20' attribute.
$args = array(
'post_type' => 'product',
'visibility' => 'visible',
'post_status' => 'publish',
'numberposts' => -1,
'meta_query' => array(
array(
'key' => 'check_key',
'value' => '1',
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy'=> 'pa_size_10_20',
'field' => 'name',
'terms' => 0,
'operator' => '!='
)
)
);
$posts = get_posts( $args );
TIA
It seems as though your tax_query would return all taxonomy terms that aren't named 0 an integer?
I think if you know the values for IN then I would use those instead.
$args = array(
'post_type' => 'product',
'visibility' => 'visible',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'check_key',
'value' => '1',
'compare' => '='
),
),
'tax_query' => array(
array(
'taxonomy'=> 'pa_size_10_20',
'field' => 'name',
'terms' => array('100', '300'),
'operator' => 'IN'
),
),
);

Exclude Tags from PHP Query

I have written the following query in my child themes functions.php file.
Currently this works exactly as it needs to however I wish to extend this query such that it excludes specific tags.
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'product_tag',
'terms' => 492,
'field' => 'id',
),
),
);
You can use 'compare' => 'NOT IN'.
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'product_tag',
'terms' => array( 492, 422 ),
'field' => 'id',
'compare' => 'NOT IN'
),
),
USEFUL LINKS
wp_meta_query

Combining meta_query and tax_query to fetch all future events (custom post)

I am using this WP query to fetch just the future events but the query returns all the events, even the past ones. What am I doing wrong? The date format is the same (j F Y) of the one coming from the ACF custom field and the tax_query is working because only the events belonging to the specified location are fetched (location_categories).
$events = array(
'post_type' => 'event',
'post_status'=>'publish',
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'DESC',
'posts_per_page'=>'6',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'location_categories',
'field' => 'term_id',
'terms' => get_field('choose_event_location',$page_id)
)
),
'meta_query' => array(
array(
'key' => 'event_start_date',
'value' => date("j F Y"),
'compare' => '>=',
'type' => 'DATE'
)
)
);
$loop = new WP_Query($events);
ACF save date as 'Ymd'. try date("Ymd")
This is how ACF saves date.
Check the below code.
$events = array(
'post_type' => 'event',
'post_status' =>'publish',
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'DESC',
'posts_per_page' =>'6',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'location_categories',
'field' => 'term_id',
'terms' => get_field('choose_event_location',$page_id)
)
),
'meta_query' => array(
array(
'key' => 'event_start_date',
'value' => date("Ymd"),
'compare' => '>=',
'type' => 'DATE'
)
)
);
$loop = new WP_Query($events);

woocommerce product attribute, how to filter between two values

Lets say i have a list off houses and they have a attribute called "size" now I want to get all houses between size 200 and 300.
I have tried
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'pa_size',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
);
Then I tried with tax_query but I couldn't find a way to get a term between two values.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => $sizevalue
)
);
);
Can't understand if this should not be possible but I think the value has to be a string therefor it cant be between.
for now im sorting them in my foreach loop when im displaying them but then my pagination is not working.
My conclusion was that you cant do this with woocommerce product attributes because they a text based, så I made some Advancec custom fields and used them insted like this
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND,
array(
'key' => 'myCutsomField',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'myCutsomField2',
'value' => array($valueMin, $valueMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
$products = new WP_Query( $args );

WP_Query not ordered correctly

I have a custom post type cota where all data is displayed in a <table> in my site. I need to order this table by:
administradora - some kind of "category" that the user specifies. Ordered ASC (a-z);
valor - The price of the cota. Each cota has a price. Ordered DESC (9 - 0) ;
The problem : It's already ordered by administradora, but not correctly ordered by valor.
Example: The order by valor displays first a cota with US$9.000,00 and then another cota with US$1.000.000,00. It's wrong. The one million dollar cota should come first, and then the nine thousand dollars cota.
Here's an image that shows better the situation
Code for ordering the cota:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'slug',
'terms' => $atts['tipo'],
),
),
'orderby' => 'meta_value_num',
'meta_query' => array(
'relation' => 'AND',
'valor_clause' => array(
'key' => 'valor',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'valor_clause' => 'DESC'
)
)
);
EDIT 1: After some research, i found out that wordpress saves all its data as longtext in the database. But still cannot solve the problem.
I can provide more info if needed.
Try Below Code:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'id',
'terms' => $atts['tipo'],
),
),
'orderby' => 'meta_value_num',
'meta_query' => array(
'relation' => 'AND',
'valor_clause' => array(
'key' => 'valor',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'valor_clause' => 'DESC'
)
)
);
SOLVED
The following code should work:
$query = new WP_Query( array(
'post_type' => 'cota',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tipo',
'field' => 'slug',
'terms' => $atts['tipo'],
),
),
'meta_query' => array(
'relation' => 'AND',
'credito_clause' => array(
'key' => "valor",
'orderby' => 'meta_value_num',
'type' => 'DECIMAL',
'compare' => 'EXISTS'
),
'adm_clause' => array(
'key' => 'administradora',
'compare' => 'EXISTS'
)
),
'orderby' => array(
'adm_clause' => 'ASC',
'credito_clause' => 'DESC'
)
)
);

Categories