skip to Main Content

I try to make a calendar for my wordpress site and I have a code that shows fine.

I run into the fact that I want to add a custom field to posts with the same date. All the loops in my current code make it too complex for me.

I’ve already tried a lot and thought I was close to it every now and then, but now I’m stuck.
Can someone help me further?

<?php
$year = date('Y');
$custom_field_key = 'opbrengst'; // Replace with the key of the custom field you want to get the value from
$monthNames = array(
    1 => 'Januari',
    2 => 'Februari',
    3 => 'Maart',
    4 => 'April',
    5 => 'Mei',
    6 => 'Juni',
    7 => 'Juli',
    8 => 'Augustus',
    9 => 'September',
    10 => 'Oktober',
    11 => 'November',
    12 => 'December'
);

for ($month = 1; $month <= 12; $month++) {
    $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $monthName = $monthNames[$month];
    echo "<div class='month'>";
    echo "<table class='calendar table-flex'>";
    echo "<tr><th colspan='7'>$monthName</th></tr>";
    echo "<tr><td>Ma</td><td>Di</td><td>Wo</td><td>Do</td><td>Vr</td><td>Za</td><td>Zo</td></tr>";

    for ($i = 1; $i <= $daysInMonth; $i++) {
        $dayOfWeek = date('N', strtotime("$year-$month-$i"));
        if ($i == 1) {
            echo "<tr>";
            for ($j = 1; $j < $dayOfWeek; $j++) {
                echo "<td></td>";
            }
        }
        $date = "$year-$month-$i";
        $args = array(
            'date_query' => array(
                array(
                    'year' => $year,
                    'month' => $month,
                    'day' => $i,
                ),
            ),
            'posts_per_page' => -1,
        );
        $query = new WP_Query($args);
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                $post_id = get_the_ID();
                $custom_field_value = get_post_meta($post_id, $custom_field_key, true);
                // set background color
                $custom_field_value > 0 ? $color = "green" : $color = "red";

                if ($dayOfWeek == 7) {
                    echo "<td class="$color">$i<br>$ $custom_field_value</td></tr><tr>";
                } else {
                    echo "<td class="$color">$i<br>$ $custom_field_value</td>";
                }
            }
        } else {
            if ($dayOfWeek == 7) {
                echo "<td>$i</td></tr><tr>";
            } else {
                echo "<td>$i</td>";
            }
        }
    }

    echo "</tr></table>";
    echo "</div>";
}
?>

2

Answers


  1. Chosen as BEST ANSWER

    This is a working example as I want it, but I can't put it together in my calendar.

    <?php
    $custom_field_key = 'opbrengst';
    
    $startDatum = "2023-01-01";
    $eindDatum = "2023-12-30";
    
    $args = array(
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'datum',
                'type' => 'DATE',
                'value' => array($startDatum, $eindDatum),
                'compare' => 'BETWEEN'
            ),
        )
    );
    
    
    $query = new WP_Query( $args );
    $sum = 0;
    $prev_date = '';
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            $date = get_the_date( 'Y-m-d' );
            if ( $prev_date == '' || $prev_date != $date ) {
                if ( $prev_date != '' ) {
                    echo '<p>Sum of custom field for date '.$prev_date.': '.$sum.'</p>';
                }
                $sum = 0;
                $prev_date = $date;
            }
            $sum += get_post_meta( get_the_ID(), $custom_field_key, true );
        }
        echo '<p>Sum of custom field for date '.$prev_date.': '.$sum.'</p>';
    }
    wp_reset_postdata();
    

  2. To add a custom field to posts with the same date, you can modify the code to include a check for the custom field value for each post that matches the current date in the loop that retrieves the posts. If the custom field value matches a certain value, you can add the post ID to an array, and then display the custom field value for all posts in the array.

    Here’s an example modification to the code:

    <?php
    // Set the year to the current year
    $year = date('Y');
    
    // Set the key of the custom field you want to get the value from
    $custom_field_key = 'opbrengst';
    
    // Define an array of month names in Dutch
    $monthNames = array(
        1 => 'Januari',
        2 => 'Februari',
        3 => 'Maart',
        4 => 'April',
        5 => 'Mei',
        6 => 'Juni',
        7 => 'Juli',
        8 => 'Augustus',
        9 => 'September',
        10 => 'Oktober',
        11 => 'November',
        12 => 'December'
    );
    
    // Loop through each month
    for ($i = 1; $i <= $daysInMonth; $i++) {
            $dayOfWeek = date('N', strtotime("$year-$month-$i")); // get the day of the week (1 for Monday, 7 for Sunday) for the current date
            if ($i == 1) { // if it's the first day of the month, start a new row in the table
                echo "<tr>";
                for ($j = 1; $j < $dayOfWeek; $j++) { // add empty cells for the days before the first day of the month
                    echo "<td></td>";
                }
            }
            $date = "$year-$month-$i"; // create a date string in the format yyyy-mm-dd
            $args = array( // set up query arguments to get posts for the current date
                'date_query' => array(
                    array(
                        'year' => $year,
                        'month' => $month,
                        'day' => $i,
                    ),
                ),
                'posts_per_page' => -1, // show all posts for the current date
            );
            $query = new WP_Query($args); // create a new WP_Query object with the query arguments
            if ($query->have_posts()) { // if there are posts for the current date
                while ($query->have_posts()) {
                    $query->the_post(); // set up the post data
                    $post_id = get_the_ID(); // get the post ID
                    $custom_field_value = get_post_meta($post_id, $custom_field_key, true); // get the value of the custom field for the current post
                    // set background color based on custom field value (green for positive, red for negative or zero)
                    $custom_field_value > 0 ? $color = "green" : $color = "red";
    
                    if ($dayOfWeek == 7) { // if it's Sunday, end the row and start a new one
                        echo "<td class="$color">$i<br>$ $custom_field_value</td></tr><tr>";
                    } else { // otherwise, just add a new cell
                        echo "<td class="$color">$i<br>$ $custom_field_value</td>";
                    }
                }
            } else { // if there are no posts for the current date
                if ($dayOfWeek == 7) { // if it's Sunday, end the row and start a new one
                    echo "<td>$i</td></tr><tr>";
                } else { // otherwise, just add a new cell
                    echo "<td>$i</td>";
                }
            }
        }
    

    I hope this helps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search