reuse wp query with different meta query - php

I am querying a custom post type. I would like to output posts which fall after a custom date field which I can do fine. But then I want to reuse the same basic query to output posts which fall before the custom date field.
So I have
$today = date('Ymd');
$args = array(
'post_type' => array( 'mypoststype' ),
'posts_per_page' => '3',
'meta_query' => array(
array(
'key' => 'mypoststype_date',
'compare' => '<=',
'value' => $today,
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$myposts= new WP_Query( $args );
then I output the necessary contents in a loop. All fine.
Next I want to have posts from the same type ordered by the same key but with compare set to '>='.
Is there some way to do this without calling a whole new query?

Related

WP_Query and acf date field does not sort right

I am trying to only see posts after or equal to today and sort ASC, my date field outputs Ymd, the first part (only see posts after or equal to today) works but the sorting does not.
$today = date ('Ymd');
$args = array(
'post_type' => 'diensten',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'datum_dienst',
'value' => $today,
'compare' => '>='
)
),
'meta_key' => 'datum_dienst',
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
For the few people facing the same problem, here is my very specific solution.
If you have the wordpress plugin Post Types Order installed it will override your order query, i uninstalled it and everything magically worked!

Display Wordpress post list, sorted by an ACF field using a shortcode

I need to display a list of posts, ordered by an ACF date picker field (end_date). This is essentially an ‘ending soon’ list. I’m using the Divi theme and so would like to have this as a shortcode I can use to place the list anywhere within the builder.
My PHP is pretty basic so any help would be greatly appreciated.The below did not work, probably I’m sure for very obvious reasons that are above my ability.
function sort_query_order( $query ) {
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$wp_query = new WP_Query( array (
'post_type' => 'post',
'meta_query'=> array(
array(
'key' => 'end_date',
'compare' => '>',
'value' => $currentdate,
'type' => 'DATE',
)),
'meta_key' => 'end_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 12,
'paged' => $paged,
)
);
}
add_shortcode( 'ending_posts', 'sort_query_order' );

Wordpress query based on ACF Datepicker values results in no posts

So I added some custom fields to the post posttype, namely event_startdate and event_enddate. My goal is to show all events that are currently ongoing (so current date is between start and enddate). However, no posts are showing up with my current code.
This is what I've got:
<?php
$today = date('Ymd');
$args = array (
'posts_per_page' => '9',
'order' => 'ASC',
'orderby' => 'id',
'meta_query' => array(
array(
'key' => 'event_startdate',
'compare' => '>=',
'value' => $today
),
array(
'key' => 'event_enddate',
'compare' => '<=',
'value' => $today
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
and then the rest of the loop. I know the loop works, because all posts show up when the meta_query is removed. What could the issue be? I've experimented with some different dateformats (hardcoded), but that didn't seem to fix it. Tried other solutions posted online as well, but none seemed to fix the issue.
EDIT: Started working on another query in the meantime, this time using event_featured, a TRUE/FALSE field. Code is the same as the previously mentioned code, except for the args. Doesn't return posts either:
$args = array (
'post_type' => 'post',
'posts_per_page' => '9',
'order' => 'ASC',
'orderby' => 'id',
'meta_query' => array(
array(
'key' => 'event_featured',
'value' => '1',
'compare' => '=='
)
)
);
I did not see in your code where you declared the post type to query. Add the post type to the query, see the first line after the first array. Try use this:
$args = array (
'post_type' =>'posttype',
'posts_per_page' => '9',
'order' => 'ASC',
'orderby' => 'id',
'meta_query' => array(
array(
'key' => 'event_startdate',
'compare' => '>=',
'value' => $today
),
array(
'key' => 'event_enddate',
'compare' => '<=',
'value' => $today
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
Found the issue. I created the fields in PHP. Using the admin interface to create them worked for me. So if you're also struggling with this, delete your fields in php and create them again in Wordpress.
If you want to include them with your theme, export the fields to PHP code with the export tool in ACF.

Custom wordpress meta query (no result)

Problem
I am looping through custom post types (Advanced Custom Fields) in Wordpress. I only want to display events with start_date equal to $newdate variable, defined in the beginning.
The start_date is in the format YYYY-MM-DD HH:mm (the same as $newdate). $newdate is set to the beginning of the day so I wouldn't exclude events with different hours in the day and compare is set to greater than (just to test the query).
However I am not getting any results.
<?php
$newdate = date('Y-m-d 00:00');
//<-- Start the Loop. -->!
$args = array(
'post_type' => 'epsa_events',
'posts_per_page' => 5,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array (
array(
'key' => 'start_time',
'value' => $newdate,
'compare' => '>=',
'type' => 'datetime'
)
)
);
$loop = new WP_Query( $args );
Try this query:-
'meta_key' => 'event-start-date',
'meta_query' => array (
array(
'key' => 'start_time',
'value' => date('Ymd',strtotime($newdate)),
'compare' => '>=',
'type' => 'date'
)
)
I guess you have some small mistakes here.
First, if you use 'orderby' => 'meta_value' you must add a 'meta_key' => KEYVALUE to your $args where KEYVALUE is the name of the custom field you want to use for sorting. Check WP_Query documentation on Order & Orderby Parameters.
Second, assuming your start_date field type is Date Time Picker, if you want to get all events that already started you're using the wrong comparison operator. So assuming you also want to sort by date, your $args code should be:
$args = array(
'post_type' => 'epsa_events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_key' => 'start_time',
'meta_type' => 'DATETIME'
'meta_query' => array (
array(
'key' => 'start_time',
'compare' => '<=',
'value' => $newdate,
'type' => 'DATETIME'
)
)
);
Take a look at Date Time Picker documentation too.
I hope this helps!

Displaying only custom post types that's custom date fields values are either today or in the future

I'm using the following custom WordPress query to display data (events) from a custom post type on the homepage of my site:
$posts = new WP_Query(array('post_type' => 'events',
'posts_per_page' => '3',
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
This works as expected,however, as the nature of events is that they are always in the future i have a requirement that within the above query i need to filter out any post that's custom field 'event_date' is in the past, I also still wish to display, events occurring on the current day.
I've tried post_status => future but this only displays unpublished posts whereas all of my events are already published. Any insight is much appreciated Thank you
You should use meta_query custom field parameters.
$args = array(
'post_type' => 'events',
'posts_per_page' => '3',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => date( YOUR-FORMAT-HERE ),
'compare' => '>='
)
)
);
$query = new WP_Query( $args );
Hope it helps!

Categories