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
}
Related
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.
I need to display "Featured posts" randomly for each day of the week on a single page. Posts need to be current.
What I want to do:
I load 7 posts in a variable using WP_Query, test each post to see if it's current, and display only the remaining ones.
Problem: I can't find how to delete an entry in a while loop.
Init of the page:
$args = array(
'post_type' => 'listing',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
'orderby' => 'date',
'order' => 'DESC',
'meta_key' => 'featured_calendar',
'meta_value' => 'on',
'posts_per_page' => 7
);
$featured = new WP_Query($args);
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();
$theending = get_post_meta($post->ID, 'theend', true);
if ($theending > time()){
echo "All Good";
} else{
//Delete the entry
}
endwhile;
endif;
Inside the days of the week loop:
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();
//Display of remaining posts
endwhile;
endif;
It sounds like you should just modify the query to exclude those posts in the first place:
$args = array(
'post_type' => 'listing',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'featured_calendar',
'value' => 'on',
'compare' => '='
),
array(
'key' => 'theend',
'value' => time(),
'compare' => '>',
'type' => 'NUMERIC' // Not sure if you want a TIME here
),
),
'posts_per_page' => 7
);
$featured = new WP_Query($args);
Read more about custom field parameters in the Codex.
If you're looking to remove a row from an array, you can do that like this:
unset ($array[$key]);
$recent_featured = array();
$featured = new WP_Query($args);
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();
$theending = get_post_meta($post->ID, 'theend', true);
if ($theending > time()){
$recent_featured[] = $post;
} else{
}
endwhile;
now you can use $recent_featured, pass it, loop over it, display them...
I'm trying to get the post, meta datas on table wp_postmeta. I need to get the post id, meta keys and meta value by using the meta_key and post id. The meta key stored is date. eg,. 2014-01-02, 2014-02-03, I have to query them based on the year or month.
$args = array(
'post_type' => 'calendar_holiday',
'ID' => $id;
'meta_query' => array(
array(
'key' => ,
'value' => ,
'compare' => 'LIKE'
),
)
);
$query = new WP_Query( $args );
I'm not sure what to put on key and value.. any idea?? thanks
// 'orderby' => 'meta_value_num', // it's a numeric value
$args = array(
'orderby' => 'meta_value',
'post_type' => 'calendar_holiday',
'meta_query' => array(
array(
'key' => 'over-make',
'value' => 'Doral',
'compare' => 'LIKE'
)
)
);
$query = new WP_Query( $args );
I have this wp query...
$downloads = new WP_Query(array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
));
But I want to add this to the query if my $user_admin condition is true...
if ($user_admin)
$downloads = new WP_Query(array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
)
);
So I run this it seems to break my loop, but not cause a fatal error...
$downloads = new WP_Query(array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
));
if ($user_admin) {
$downloads = new WP_Query(array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
));
}
OK my question is essentially this... How do I blend the two $downloads variables if the $user_admin condition equals true.
But the fastest and correct method of actually going about doing this as my method does not work.
From looking at your code it looks like you could merge the array together and then create the new WP_Query object. From your description I understand that you are saying that you want the queries to be blended into one and not the results of the query blended into one.
$args = array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
);
if ($user_admin) {
$args = array_merge($args, array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
));
}
$downloads = new WP_Query($args);
I also was wondering you indicated that your current code seems to break your loop. Exactly what is happening with your first code example. Are you getting a blank page or simply not having any articles returned?
Another thing to note when checking if a user is an administrator you can also use is_admin() instead of $user_admin.
Function Reference/is admin
Try:
$query = array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
);
if ($user_admin) {
$query2 = array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
);
$query = array_merge($query, $query2);
}
$downloads = new WP_Query($query);
Hope this helps!
So here is my query:
$args = array(
'post_type' => 'Event',
'posts_per_page' => 1000,
'meta_key' => 'event_informations_show_on_the_homepage',
'meta_value' => 'Show on the homepage',
'meta_compare' => '==',
'meta_key' => 'event_informations_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$loop = new WP_Query( $args );
I want to select all posts that have the metabox event_informations_show_on_the_homepage and the value of the metabox event_informations_show_on_the_homepage and order by the date metabox which is stored as a timestamp and is called event_informations_date.
What am I doing wrong?
Hopefully I'm not barking up the wrong tree here.
You can use the key 'meta_query' to filter posts by multiple meta keys like so:
$args = array(
'post_type' => 'Event',
'posts_per_page' => 1000,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'event_informations_show_on_the_homepage',
'value' => 'yes',
),
array(
'key' => 'event_informations_date',
'value' => 'yes',
)
)
);
$query = new WP_Query( $args );
What WordPress is doing here is creating multiple wheres against the same column by using innerjoins on the same table, each time using a different alias. It's pretty cool & is probably the fastest way to query like that.
For more information see here: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Hope this helps :)