Monday 15 July 2013

wordpress - Ignore posts from before today PHP -



wordpress - Ignore posts from before today PHP -

i have next loop in wordpress , though worked (at least, think so), stopped working should:

<?php $items = 0; $thiscat = get_category(3); $cat_slug = $thiscat->slug; $args = array( 'post_type' => 'course', 'posts_per_page' => 3, 'meta_key' => 'date_start', 'orderby' => 'meta_value', 'category_name' => $cat_slug, 'order' => 'asc', ); ?> <ul> <?php $loop = new wp_query( $args ); while ( $loop->have_posts() , $items < 3) { $loop->the_post(); $category_course = get_the_category(3); global $post; $category_detail = get_the_category( $post->id ); $date_start = get_post_meta(get_the_id(), 'date_start', true); $place = get_post_meta(get_the_id(), "place", true); if( $date_start >= strtotime('today') ) { ?> <li> <a href="<?php the_permalink(); ?>" class="date_fp"><?php echo strftime("%a, %e. %b. %y", $date_start); ?> - <?php echo $place;?></a> <?php foreach ($category_detail $category_id) { if($category_id->cat_name == 'z_aflyst') { echo "- <strong>aflyst</strong>"; } }?> </li> <?php $items++; } } if($items==0) { ?> <li> ingen kommende kurser </li> <?php } ?>

the expected result: count courses held in future, display maximum of 3 on front end page

the outcome: count courses in database (both past , present) maximum of 3, display ones held on front end page (the ones held in past not displayed, counted). if 3 or more courses held in past, doesn't display of courses held in future.

in head, should ignoring posts date before today, apparently still counts them, output on front end page 1 course of study (in case of above loop, there 2 courses held in past , 3 planned future), instead of available 3. found out if alter posts_per_page 4, it'll display 1 more course, has fact counts courses in past post in posts_per_page.

it's little tweak of code, how , create sure ignores posts before strotime('today')?

by adapting $args's meta-query, managed compare value of meta-key value of choosing (in case time()).

by adding this, possible remove if-sentence find out if course of study placed in future or past.

$args['meta_query'] = array( array( 'key' => 'date_start', 'value' => time(), 'compare' => '>=' ) )

this results in $args variable grabbing posts equal or greater time(). epic solution stackoverflow, made code more readable.

php wordpress

No comments:

Post a Comment