Wordpress - managing event listings? - php

I have an events page that uses the following query:
<?php $portfolioloop = new WP_Query( array( 'post__not_in' => array(4269), 'paged' => get_query_var('paged'), 'post_status' => 'future', 'post_type' => 'whatson', 'exclude' => '4269', 'posts_per_page' => 20, 'order' => 'ASC')); ?>
All this does is show a list of all scheduled custom posts and when the post hits the scheduled date it publishes the page... thus removing it from the list.
It's nearly what I want, when it hits the publish date, the event is actually running on that day so removing it from the list isn't quite correct.
Is there a way I can delay removing it from the list until the end of the day?
p.s I don't want to use a plugin as I don't think it warrants it.
I've found this:
$args = array(
'posts_per_page' => 3,
'meta_key' => 'event-start-date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array( 'key' => 'event-end-date', 'compare' => '>=', 'value' => date('Y-m-d') )
)
);
query_posts($args);
I don't want to sort by a custom field so how can I do it by the post publish date?

Can't you just add a post_date WHERE statement to the query to search for posts? Then the post_status will have to be removed from the query, thus:
<?php $query_string = array( 'post__not_in' => array(4269), 'paged' => get_query_var('paged'), 'post_type' => 'whatson', 'exclude' => '4269', 'posts_per_page' => 20, 'order' => 'ASC'); ?>
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
$end_of_day = date('Y-m-d') . ' 23:59:59';
$where .= " AND post_date < '$end_of_day' ";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );

Finally solved this with:
function my_filter_where( $where = '' ) {
global $wp_query;
if (is_array($wp_query->query_vars['post_status'])) {
if (in_array('future',$wp_query->query_vars['post_status'])) {
// posts today into the future
$where .= " AND post_date > '" . date('Y-m-d', strtotime('now')) . "'";
}
}
return $where;
}
add_filter( 'posts_where', 'my_filter_where' );
And:
<?php
$wp_query = array(
'post__not_in' => array(4269),
'paged' => get_query_var('paged'),
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 20,
'order' => 'ASC',
'orderby' => 'date',
'post_status' =>array('future','published'));
query_posts($wp_query);
?>
<?php
if ($wp_query->have_posts()) {
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
Content
<?php endwhile; // end of the loop.
} ?>
<?php if (function_exists('wp_pagenavi')) { wp_pagenavi( array( 'query' => $wp_query ) ); } ?>

Related

WordPress Query display only posts modified today

I'm trying to create a query in WordPress that displays only the posts that were edited today, excluding those posted today. I've tried several variations but nothings seem to be working:
$today = current_time('Ymd');
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '10',
'meta_query' => array(
array(
'key' => 'modified',
'compare' => '>=',
'value' => $today,
'type' => 'NUMERIC,'
)
),
'orderby' => 'modified',
'order' => 'DESC',
'ignore_sticky_posts' => '1'
);
I'm not quite sure what to put in key, although that isn't the only problem.
If I get it right, with "displays only the posts that were edited today, excluding those posted today."
I guess you mean display ONLY old published posts modified/edited today.
If that is the case, this might help you:
<?php
// query args
$args = array(
'posts_per_page' => '10',
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'modified',
'order' => 'DESC',
'ignore_sticky_posts' => '1',
'caller_get_posts' => 1
);
// query
$updated = new WP_Query($args);
// loop
while($updated->have_posts()) : $updated->the_post();
$today = current_time('Y-m-d'); // current date a.k.a. TODAY
$pub = get_the_time('Y-m-d', $updated->ID); // date when post was published
$mod = get_the_modified_time('Y-m-d', $updated->ID); // date when post was last modified
// if post NOT published today AND was modified today display:
if ( $pub !== $today && $mod === $today ) :
?>
<!-- here goes your normal wp game -->
<h1><?php the_title ?></h1>
<span><?php the_date(); ?></span>
<p><?php the_excerpt(); ?></p>
<?php endif; endwhile; ?>
Its not the best solution but you can just do the filter after your query and check if the current date string is inside the post date modified,
e.g.
$ar = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '10',
'orderby' => 'modified',
'order' => 'DESC',
'ignore_sticky_posts' => '1'
);
$q = new WP_QUery( $ar );
$p = $q->get_posts();
foreach( $p as $a ) {
$c = current_time( 'Y-m-d' );
if ( strpos( $a->post_modified, $c ) !== false ) {
_e( $a->post_title .' '.$a->post_modified. ' - ' . $c. "<br>" );
}
}
#echo '<pre>', print_r($p, 1), '</pre>';
Based on this query to select all posts either published or modified today, you could just write this WP_Query to retrieve only the modified ones:
$args = array(
'post_type' => 'post',
'post_status' => 'any', // we also want the drafts
'nopaging'=>true,
'date_query' => array(
'column' => 'post_modified',
'year' => $day_parsed['year'],
'month' => $day_parsed['month'],
'day' => $day_parsed['day'],
)
);
$query_day_posts = new WP_Query( $args );

PHP: Delete entry in array of arrays

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...

How do I add order & orderby parameters to this wordpress query?

I managed to paint myself into a corner when using this snippet, but I can't manage to work out how to sort the query. Usually I can do it but with this snippet that excludes one tag on the tag page I can't really work it out. Anyone?
$exclude_tags = array(17);
global $wp_query;
$wp_query->set('tag__not_in', $exclude_tags);
$wp_query->get_posts();
if (have_posts()) : while (have_posts()) : the_post();
This example will be help full for you :-
$args = array(
'post_type' => 'post',
'meta_key' => 'pb_issue_featured',
'orderby' => 'meta_value',
'order' => 'DESC',
'posts_per_page' => $posts,
'paged' => $paged,
'paged' => 1,
'meta_query' => array(
array(
'key' => 'headline',
'value' => 1,
'compare' => '!='
)
)
);
add_filter( 'posts_orderby', 'filter_query' );
$q = new WP_Query($args);
remove_filter( 'posts_orderby', 'filter_query' );
function filter_query( $query ) {
$query .= ', wp_posts.menu_order ASC';
return $query;
}
Referenced From
Please look at this example. You can do like this.
The code will display the title of last ten posts sorted alphabetically in ascending order.
<?php
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
<div>
<?php the_title(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
Please refer the link http://codex.wordpress.org/Template_Tags/get_posts for more details.

PHP: Doing while loop if "if" statement is true

I am trying to show only those events, which are in current date or in future. To show events I have such code:
<?php
$wp_query = new WP_Query();
global $more;
// set $more to 0 in order to only get the first part of the post
$more = 0;
$wp_query->query( array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
while ( ($wp_query->have_posts()) ): $wp_query->the_post();
get_template_part( 'content', 'event' );
endwhile;
?>
I tried to add "IF statement" to compare event start date with current date, but it doesn't seem to work. I have tried a lots of versions, but this one I think should work:
$wp_query->query( array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
$OstartDate = get_post_meta($post->ID, '_event_start', TRUE);
$today = date('d.m.Y');
while ( ($wp_query->have_posts()) && ($OstarDate < $today) ):
$wp_query->the_post();
From the second one you should try,
$OstartDate=get_post_meta($post->ID,'_event_start',TRUE);//check it will return a number
if(time() > $OstartDate)
{
while ($wp_query->have_posts()):
$wp_query->the_post();
.....
}
a7-simple-events may help you.
You can compare the posts with postmeta using meta_query
$today = date('d.m.Y');
$args=array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_query' => array(
array(
'key' => '_event_start',
'value' => $today,
'type' => 'date',
'compare' => '<'
)
)) ;
$wp_query = new WP_Query( $args );
while ( ($wp_query->have_posts()) ):
$wp_query->the_post();
endwhile;

Showing upcoming events (including todays event)?

I have the following loop that doesn't quite work. I want to only show upcoming events including events that are taking place today.
At the moment it shows all the upcoming posts but also the posts before todays date.
Where am I going wrong?
<?php
$today = date('Ymd');
$portfolioloop = new WP_Query(
array(
'post__not_in' => array(4269),
'paged' => get_query_var('paged'),
'meta_key' => the_date(),
'post_status' => 'future,publish',
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 20,
'order' => 'ASC',
'meta_query' => array(
array('key' => the_date(),
'value' => $today,
'compare' => '>=')
),
)); ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
// content here.
<?php endwhile; // end of the loop. ?>
I'll give this a go:
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
$today = date('Ymd');
$where .= " AND post_date >= '$today' ";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
<?php
$query = new WP_Query(
array(
'post__not_in' => array(4269),
'paged' => get_query_var('paged'),
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 20,
'order' => 'ASC'
)); ?>
I think this might work:
<?php
function filter_where( $where = '' ) {
$where .= " AND post_date >= '" . date("Y-m-d") . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query(
array(
'post__not_in' => array(4269),
'paged' => get_query_var('paged'),
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 20,
'order' => 'ASC'
)
);
remove_filter( 'posts_where', 'filter_where' );
?>

Categories