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.
Related
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!
I have a set of wordpress postst that have various metafields. I'm trying to filter through these posts depending on the arguments given in a url. For example.
www.example.com/?artist=franksinatra&mood=
Should bring up all posts with frank sinatra as the artist field, but ignoring the mood field because its blank. That works fine. But I'd like to narrow my search even further. So when I add additional arguments such as:
www.example.com/?artist=franksinatra&mood=sad
All posts with that field combination should show up. Here's the beginning of my loop that allows me to search using one argument but not additional ones. Thank you.
<?php
$queryArtist = $_GET['artist'];
$queryMood = $_GET['mood'];
$paged = get_query_var('paged');
$loop = new WP_Query( array(
'posts_per_page' => 5,
'post_type' => 'catalog',
'paged' => $paged,
'order_by' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'artist',
'value' => $queryArtist,
'compare' => '=',
)
)
)
);
while ( $loop->have_posts() ) : $loop->the_post(); ?>
you can add additional queries like this:
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'artist',
'value' => $queryArtist,
'compare' => '=',
),
array(
'key' => 'mood',
'value' => $queryMood,
'compare' => '=',
)
)
by switching the relation from "OR" to "AND" both conditions have to be true
Using get_posts(), I need to first retrieve posts that fall on a certain day (the day is set by a custom field - just the date, not time). I do this by using a meta key/value. Then, I need to order these posts based on the time of day (which is a separate custom field, just time, not date). So essentially I need to pull in all the events that fall on a given day, and order them according to the time.
First I grab the day, using a custom field:
if ( get_field('festival_day') ) {
$day_stamp = get_field('festival_day');
}
Then I set my arguments for the query:
$args = array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_key' => 'event_date',
'meta_value' => $day_stamp
);
$events = get_posts( $args );
So.. the question is, how do I query the other custom field (which is the start time), and then sort by that time? The time field key is event_start_time.
Thanks!
You can user WP_Query to retrieve your events and you can query it something like below:
$args = array(
'post_type' => 'event',
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'event_start_time',
'meta_query' => array(
array(
'key' => 'event_start_time',
'value' => 'yourValue_here',
'compare' => '>='
),
array(
'key' => 'event_date',
'value' => $day_stamp,
'compare' => '='
)
)
);
$query = new WP_Query( $args );
UNTESTED but it should work.
Check this code, it should work to sort your post according to time from resulting post of day.
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'event_start_time',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'event_date',
'value' => $day_stamp,
'compare' => 'IN'
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>'. get_the_title().'</li>';
}
echo '</ul>';
} else {
// no posts found
}
My post type is product. I use a checkbox field with meta key is ht_featured, meta value when I print_r is array([0] => featured).
My WP_Query:
$the_query = new WP_Query(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
);
It doesn't show any post.
I tried with value => 'featured' and 'compare' => 'EXISTS' but it not working.
WP_query needs to be passed in an array. use following code and let me know if any prob.
$the_query = new WP_Query (array (
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
));
You can refer to the discussion at wordpress forum:
http://wordpress.org/support/topic/how-to-wp_query-meta_query-value-string-contain-in-key-string
You're passing all of this into WP_Query as individual arguments when they should be contained in an array.
$the_query = new WP_Query( array(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN',
),
),
) );
Can you clarify your point about the checkbox? I'd suggest simply updating 'ht_featured' with either 'yes' or 'no' when you save the product. Then change your 'value' in the meta query to 'yes' and remove the 'compare'.
Are you sure there is no php error?
I think WP_Query needs to be passed in an Array
$the_query = new WP_Query(
array(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
));
I had a similar problem until I used the function get_posts() rather than creating a new WP_Query. See if that helps...
I will try and explain this a simple as possible.
I have 3 post types that come into play here.
reports
events (motogp-2013, motogp-2014)
circuits
On my 'events' single.php to display the event information, I am trying to create a query to display related 'reports'.
And the only relation that the event content can have with a report is the circuit.
When I create a 'report', I have to assign an 'event' to it. But when I create a 'event' prior to a report, I have to assign a 'circuit' to it.
For example, it looks like this...
> 'report' - Second place for Pedrosa as black flag terminates race for Marquez (1023)
> 'event' - Australian Grand Prix (662)
> 'circuit' - Phillip Island (156)
So I am trying to query 'reports', which had 'events' at specific 'circuits'.
On my 'events' single.php, I do have the circuit id, but I don't know how to list the reports that which are at that circuit.
I can easily show related 'events' using this query
$related = new WP_Query(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
But this is not what I'm trying to achieve, I need to show related reports.
This reports_race_event meta key contains the id number of the 'event', and the 'event' contains the meta key event_calendar_circuit which contains the circuit id.
My question is how do I do this for 'reports', when the only meta_key I have is reports_race_event
I need to list 'reports' which we're held at a specific $circuit
If any one can help me that would be great thanks.
I've figured out how I can do this!
But I still need help please, I can list all related reports like this...
$related_reports = new WP_Query(array(
'post_type' => 'reports',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'reports_race_event',
'value' => $events_at_circuit,
'compare' => not sure,
'type' => not sure
)
)
));
But I need to create an array of 'event' id numbers and store it in this $events_at_circuit variable.
Buy using this query first...
global $circuit;
$related_events = get_posts(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
My question is now, how do I get the returned post id numbers from the $related_events query and store them as an array in $events_at_circuit
I eventually got there...
Here is the answer, I had to store the array numbers for the events that where at that circuit, then queried by meta_key value using an array variable. Then comparing using wordpress meta IN comparison. See full code below...
$events_at_circuit = null;
$related_events = get_posts(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
if( !empty( $related_events ) ){ foreach( $related_events as $related_event ) $events_at_circuit[] = $related_event->ID; }
$related_reports = new WP_Query(array(
'post_type' => 'reports',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'reports_race_event',
'value' => $events_at_circuit,
'compare' => 'IN',
'type' => 'NUMERIC'
)
)
));
if ( $related_reports->have_posts() ) :