WordPress: Create a Dynamic Filter Showing Posts of Numeric Value
Working in WordPress here. As you may know, all of the typical solutions for displaying upcoming events in WordPress are awful, so I'm accomplishing it with simple custom fields. Each event the user will enter they will enter a Numeric date "YYYYMMDD".
What I need to find out is how to code in PHP for the script to first get php:the_date <'YYYYMMDD'>开发者_如何学Python and then have WordPress filter events dynamically, only displaying posts that have a value greater than or equal to the numeric value printed (which would be the YYYYMMDD format of "today" which is pulled in via PHP:the_date.
Here is the wp_query I'm using:
<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=5&meta_key=event_date&orderby=meta_value&category_name=events&order=ASC');
if ($recentPosts->have_posts()) : while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
To get the date: $current_date = date('Ymd')
You should then be able to query your posts something like this:
$recentPosts = new WP_Query();
$recentPosts->query(array(
'category_name' => 'events',
'meta_key' => 'event_date',
'meta_compare' => '>=',
'meta_value' => $current_date,
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 5
));
Then you can loop through the posts as normal. If you need to do pagination it will be a bit more complex, see to the documentation for query_posts
.
精彩评论