Next two dates from array - php

My Wordpress theme allows for the addition of dates to an 'events' custom post type. The stock theme shows all dates entered however I have limited to two dates. Unfortunately it's the first two dates not the next two dates for the selected event.
So as the year passes by, I'd like php to look at the current date and then display forthcoming events ... i.e. the next two event dates from the array.
Here's my code:
<div id="event-days">
<ul>
<?php
global $va_locale;
$days = va_get_the_event_days();
$i = 0;
$len = count($days);
foreach( $days as $date_U => $term ) { ?>
<?php $date = $term->slug; ?>
<?php $display_date = $va_locale->date( apply_filters( 'va_single_event_dates_date_format', get_option( 'date_format' ) ), strtotime( $date ) );?>
<?php $times = va_get_the_event_day_times( $date ); ?>
<li><?php echo html_link( va_event_day_get_term_link( date( 'Y-m-d', strtotime( $date ) ) ), $display_date ); ?><?php echo va_get_the_event_day_time( $times, ' - ', ' # ' );
if ($i == 0) { ?>
<meta itemprop="startDate" content="<?php echo date( 'c', strtotime( $date ) ); ?>" />
<?php } ?>
</li>
<?php $i++;
if($i==2) break;
} ?>
</ul>
</div>
Thanks in advance for your help.
edit - here we go gavriel. cheers
function va_get_the_event_days( $event_id = 0 ) {
$event_id = $event_id ? $event_id : get_the_ID();
$terms = get_the_terms( $event_id, VA_EVENT_DAY );
if ( !$terms )
return array();
$days = array();
foreach ( $terms as $term_id => $term ) {
if ( $term->parent != 0 ) {
$days[ strtotime($term->slug) ] = $term;
}
}
return $days;
}

The easiest change would be to only add the future events to the array:
$now = time();
foreach ( $terms as $term_id => $term ) {
if ( $term->parent != 0 ) {
$term_timestamp = strtotime($term->slug);
if ($term_timestamp > $now) {
$days[ $term_timestamp ] = $term;
}
}
}

Related

Display each taxonomy term post under a Year and Month heading

I have a CPT - 'newsletters' and Taxonomy 'newsletter-groups'
I'm trying to display each post under it's respective term heading with all that terms posts displayed under it showing the Month and Year they were posted (by Postdate).
I thought I had something working , but noticed during testing that if a post had the same 'Month' it would wipe out the year and month for other posts in the same term and also for the terms below it , I have some images to show what I mean and the code I'm using so far.
In the images shown I've appended the post date to the filename for clarity , it would normally be just the filename.
Not Working if post date month is the same
Working if post date month is different
How can I solve this ? do I need to get all the post dates with a separate query and save that ?
Any examples appreciated
/*
* Custom Template: Archive Newsletters
*/
// Remove stuff
remove_action( 'genesis_loop', 'genesis_do_loop' );
// Add our custom loop
add_action( 'genesis_loop', 'newsletter_archive_loop' );
function newsletter_archive_loop() {
//fetch the terms for the newsletter-groups taxonomy
$terms = get_terms( 'newsletter-groups', array (
'hide_empty' => 'true',
'exclude' => 67, //excluding parent term ID 'school-newsletters' - only want to show term children of 'school-newsletters'
));
// run a query for each term
foreach( $terms as $term ) {
// Define the query
$args = array(
'post_type' => 'newsletters',
'newsletter-groups' => $term->slug ,
'posts_per_page' => -1,
);
// run the query
$query = new WP_Query( $args );
if( $query->have_posts() ) {
echo '<div class="letters">';
// output the term name in a heading tag
echo'<h4 class="term-heading">' . $term->name . '</h4>';
while ( $query->have_posts() ) { $query->the_post();
// get current month
$current_month = get_the_date('F');
// get attachements from custom field
$attachment_id = get_field('newsletter_upload');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );
// get the filesize
$filesize = filesize( get_attached_file( $attachment_id ) );
$filesize = size_format($filesize, 2);
if( $query->current_post === 0 ) {
echo '<h4 class="date">';
the_date( 'F Y' );
echo '</h4>';
} else {
$f = $query->current_post - 1;
$old_date = mysql2date( 'F', $query->posts[$f]->post_date );
if($current_month != $old_date) {
echo '<h4 class="date">';
the_date( 'F Y' );;
echo '</h4>';
}
} ?>
<li class="letters-file">
<?php the_title(); ?><span class="letters-file-sz"> (<?php echo $filesize; ?>)</span>
</li>
<?php } // endwhile have posts
echo '</div>'; // close letters div
// use reset postdata to restore orginal query
wp_reset_postdata();
} // end if query have posts
} // end for each
} // end function
genesis(); // call genesis framework
Remove if and else both conditions and try to print only echo ''; the_date( 'F Y' ); echo ''
it will only show the individual post dates - But not grouped
EXPECTED OUTPUT
You can push each month in an array and check if the month is already in the array then do not display repeat month.
/*
* Custom Template: Archive Newsletters
*/
// Remove stuff
remove_action( 'genesis_loop', 'genesis_do_loop' );
// Add our custom loop
add_action( 'genesis_loop', 'newsletter_archive_loop' );
function newsletter_archive_loop() {
//fetch the terms for the newsletter-groups taxonomy
$terms = get_terms( 'newsletter-groups', array (
'hide_empty' => 'true',
'exclude' => 67, //excluding parent term ID 'school-newsletters' - only want to show term children of 'school-newsletters'
));
// run a query for each term
foreach( $terms as $term ) {
$dates = array();
// Define the query
$args = array(
'post_type' => 'newsletters',
'newsletter-groups' => $term->slug ,
'posts_per_page' => -1,
);
// run the query
$query = new WP_Query( $args );
if( $query->have_posts() ) {
echo '<div class="letters">';
// output the term name in a heading tag
echo'<h4 class="term-heading">' . $term->name . '</h4>';
while ( $query->have_posts() ) { $query->the_post();
// get current month
$current_month = get_the_date('F');
// get attachements from custom field
$attachment_id = get_field('newsletter_upload');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );
// get the filesize
$filesize = filesize( get_attached_file( $attachment_id ) );
$filesize = size_format($filesize, 2);
if( !in_array(get_the_date( 'F Y' ), $dates ) ){
$dates[] = get_the_date( 'F Y' );
echo '<h4 class="date">';
echo get_the_date( 'F Y' );
echo '</h4>';
}
?>
<li class="letters-file">
<?php the_title(); ?><span class="letters-file-sz"> (<?php echo $filesize; ?>)</span>
</li>
<?php } // endwhile have posts
echo '</div>'; // close letters div
// use reset postdata to restore orginal query
wp_reset_postdata();
} // end if query have posts
} // end for each
} // end function
genesis(); // call genesis framework

How to display the rating of the current post in the WordPress loop?

There is a post type the posts of this post type are displayed with this code:
<?php
$args = array (
'post_type' => 'brands',
'posts_per_page' => -1
);
$brands = new WP_Query( $args );
?>
<?php while ( $brands->have_posts() ) : $brands->the_post(); ?>
<p class="name"><?php the_title(); ?></p>
<?php endwhile;?>
Near the title, I need to display the rating of this post. To do this, inside the loop, I write this code:
<?php
$comments = get_approved_comments( $post_id );
foreach( $comments as $comment ){
$rate = get_comment_meta( $comment->comment_ID, 'rating', true );
if( isset( $rate ) && '' !== $rate ) {
$i++;
$total += $rate;
$avrating = round($total / $i, 1);
}
}
?>
<?php
global $post;
$stars = '';
for ( $i = 1; $i <= $avrating + 1; $i++ ) {
$width = intval( $i - $avrating > 0 ? 20 - ( ( $i - $avrating ) * 20 ) : 20 );
if ( 0 === $width ) {
continue;
}
$stars .= '<span style="overflow:hidden; width:' . $width . 'px" class="dashicons dashicons-star-filled"></span>';
if ( $i - $avrating > 0 ) {
$stars .= '<span style="overflow:hidden; position:relative; left:-' . $width .'px;" class="dashicons dashicons-star-empty"></span>';
}
}
echo $stars;
?>
But as a result, I get the same values for all posts. And I need to get different values corresponding to the post, if any, and display nothing if there is no rating. Help please, I myself cannot solve this issue.
I found a solution. There may be better options, but right now my code works and solves my needs. All I've done is to place test before the code that prints rating:
<?php $comments = get_approved_comments( $post_id ); ?>
<?php if($comments == true): ?>
<?php endif; ?>
As a result, we first check to see if the post has confirmed the comments. If there is, then there is a rating (in my case, a comment cannot be left unrated). If there are more elegant solutions I'll be glad to see.

Advanced Custom Fields Wordpress Repeater Field Order By Date

I am using advanced custom fields pro to store and display information on estate sale dates. These are held in a repeater field, each row in the field has a start time, end time and a date. I need to display the information ordered by the date in the repeater field row. Currently it is ordered by date modified. I have tried several solutions but none seem to work for what I need to do.
This is what I have currently:
<?php
$latestes = new WP_Query(
array(
'post_type' => 'estate-sales',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'modified',
'order' => 'DESC'
)
);
while ( $latestes->have_posts() ) : $latestes->the_post();
echo '<li class="frontpage-esale"><a href="';
the_permalink();
echo '">';
the_title();
echo ' ';
$rowses = get_field('estate_sale_dates');
$first_row = $rowses[0];
$first_row_date = $first_row['es-date'];
$first_row_start = $first_row['es-start-time'];
$first_row_end = $first_row['es-end-time'];
echo ' - ';
if ($first_row_date) {
echo date('F j, Y',strtotime($first_row_date));
}
else {
echo 'Sale Dates TBA';
}
endwhile;
wp_reset_query();
?>
The most common answer when searching for a solution is to do something like this:
$latestes = new WP_Query(
array(
'post_type' => 'estate-sales',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'modified',
'order' => 'DESC'
)
);
while ( $latestes->have_posts() ) : $latestes->the_post();
$repeater = get_field('estate_sale_dates');
foreach( $repeater as $key => $row )
{
$column_id[ $key ] = $row['es-date'];
}
array_multisort( $column_id, SORT_ASC, $repeater );
foreach( $repeater as $row ) {
echo '<li class="frontpage-esale"><a href="';
the_permalink();
echo '">';
the_title();
echo ' ';
$rowses = get_field('estate_sale_dates');
$first_row = $rowses[0];
$first_row_date = $first_row['es-date'];
$first_row_start = $first_row['es-start-time'];
$first_row_end = $first_row['es-end-time'];
echo ' - ';
if ($first_row_date) {
echo date('F j, Y',strtotime($first_row_date));
}
else {
echo 'Sale Dates TBA';
}
}
endwhile;
wp_reset_query();
?>
However, when I try that, it lists each sale 3 times, does not order by the date at all, and if no date is entered (which needs to just default to Sale Dates TBA) it throws an error.
Any help greatly appreciated.
Note, that your integration isn't complete, signaled by having two get_field('estate_sale_dates');, and the name $first_row being out of place when iterating all of them. Without the sort, you're basically doing
$repeater = get_field( 'estate_sale_dates' );
...
foreach ( $repeater => $row_that_isnt_used )
{
$rowses = get_field( 'estate_sale_dates' );
$first_row = $rowses[0];
}
I think something like this is what you're after:
<?php
while ( $latestes->have_posts() ) {
$latestes->the_post();
echo '<li class="frontpage-esale"><a href="';
the_permalink();
echo '">';
the_title();
echo '</a> ';
if ( ! count( $sale_dates = get_field('estate_sale_dates') ) )
echo '<span>Sale Dates TBA</span>';
else
{
// $order = array_column( $sale_dates, 'es-date' ): (php 5.5+)
foreach( $sale_dates as $ignore => $row )
$order[] = $row['es-date'];
array_multisort( $order, SORT_ASC, $sale_dates );
echo "<ul>";
foreach( $sale_dates as $row ) {
$date = $row['es-date'];
$start = $row['es-start-time'];
$end = $row['es-end-time'];
echo "<li>" . date('F j, Y',strtotime($row_date)) . " $start-$end</li>";
}
echo "</ul>";
}
echo "</li>";
}
I have the same problem with a calendar. There is no solution to do this in the right way with a repater field. The best way to do this, is to use an custom field in an hidden post type and a relation field.
Because any PHP solution is inperformant.
Make a Custom Post Type "Sales Dates" an in this a custom field "date" and a second "Post Relation".
Then select from Post Type "Sales Dates" all Posts order by meta key "dates" with the relation field you get the data from the not hidden post type.

Woocommerce alphabetic list of products

I would like to have an alphabet listing the products by ABC... list
Something like
A | B | C | D .... Z
And that all link to the products-list sorted by their first letter.
I would like it placed above the list where the sorting dropdown menu is (top of the woocommerce ).
Is it possible somehow with coding, or is there some plug-in for it?
I put this at the end of orderby.php:
<div id="alphabet">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$query = new WP_Query($args);
$by_letter = array();
while( $query->have_posts() ) { $query->the_post();
global $post;
$letter = substr($post->post_name, 0, 1);
if ( ! isset($by_letter[$letter]) ) $by_letter[$letter] = array();
$by_letter[$letter][] = $post;
}
wp_reset_postdata();
?>
<?php
if ( ! empty( $by_letter ) ) {
ksort($by_letter); // order the array
// fill the array with letters have no posts
$by_letter = fill_by_letter_array( $by_letter );
display_letters_anchors( array_keys( $by_letter ) );
foreach( $by_letter as $letter => $posts ) {
?>
<div id="Productlist_<?php echo strtoupper($letter); ?>" class="Productlist_<?php echo strtoupper($letter); ?>_object productlists">
<?php
if ( ! empty($posts) ) {
foreach ( $posts as $post ) {
setup_postdata($post);
// just an example of post output
echo '<p>' . get_the_title() . '</p>';
}
} else {
echo '<p>' . __('No products.') . '</p>';
}
?>
</div>
<?php
}
wp_reset_postdata();
}
?>
</div>
A bit of css, and it Works great!

Archive Template in Wordpress

I want to create an archive page template for Wordpress that will look like this:
August 2009
Post 4
Post 3
Post 2
Post 1
July 2009
Post 2
Post 1
So, basically, I want all the posts from the blog, ordered descending by date and grouped by month.
Can someone provide me the PHP code for this?
Thanks!
PS: Wordpress version is 2.8.2
This is a function I created a while back. It basically does what you want to do, but it's not a template. Maybe you can adapt it.
<?php
/**
* Displays a condensed list of the posts grouped by month/year.
*
* #param $order The order of the posts. Either 'DESC' or 'ASC', case sensitive.
* #param $date_prefix Whether to prefix the posts with the month/date.
* #param $display Whether to display the results or return it as a String.
*/
function condensed_post_list($order='DESC', $date_prefix=true, $display=true){
global $wpdb;
if( !in_array($order, array('DESC','ASC' ) ) ) $order = 'DESC';
$query = "SELECT ID, post_title, post_date FROM $wpdb->posts ".
"WHERE post_type='post' AND post_status = 'publish' ".
"ORDER BY post_date $order";
$results = $wpdb->get_results( $query );
ob_start();
$current_month = '';
foreach( $results as $result ) {
if( $current_month != mysql2date('F Y', $result->post_date)) {
if( $current_month ) echo '</ul>';
$current_month = mysql2date('F Y', $result->post_date );
echo '<h2>'.$current_month.'</h2>';
echo '<ul>';
}
echo '<li>';
echo ($date_prefix ? mysql2date('M j: ', $result->post_date) : '');
echo '<a href="'.get_permalink($result->ID).'">';
echo $result->post_title.'</a></li>';
}
if( $current_month ) echo '</ul>';
if( $display ) {
ob_end_flush();
} else {
return ob_get_clean();
}
}
?>
I used the above function, but replaced the SQL query with:
$results = query_posts('post_type=post&post_status=publish&cat=3');
This allowed me to use the excellent function #scompt.com wrote, but limit it to a single blog category.
Thanks a lot of the help. This is what I used with the code above.
function condensed_post_list($order='DESC', $date_prefix=true, $display=true)
{
if( !in_array($order, array('DESC','ASC' ) ) ) $order = 'DESC';
$args = array(
'numberposts' => -1,
'orderby' => 'post_date',
'post_type' => 'post',
'post_status' => 'publish');
$results = get_posts($args);
ob_start();
$current_month = '';
foreach( $results as $result ) {
if( $current_month != mysql2date('F Y', $result->post_date)) {
if( $current_month ) echo '</ul>';
$current_month = mysql2date('F Y', $result->post_date );
echo '<h2>'.$current_month.'</h2>';
echo '<ul>';
}
echo '<li>';
echo ($date_prefix ? mysql2date('M j: ', $result->post_date) : '');
echo '<a href="'.get_permalink($result->ID).'">';
echo $result->post_title.'</a></li>';
}
if( $current_month ) echo '</ul>';
if( $display ) {
ob_end_flush();
}
else {
return ob_get_clean();
}
}

Categories