Home:ALL Converter>WordPress Custom Post Type Calendar with ACF Date Fields

WordPress Custom Post Type Calendar with ACF Date Fields

Ask Time:2018-04-23T06:55:48         Author:TechRemarker

Json Formatter

Since the WordPress built-in calendar does not support custom post types, using a third party version https://wordpress.org/plugins/cpt-calender-widget/. It pulls in the date based on the published date rather than the start and end date set using the Date Picker with Advanced Custom Fields (ACF).

Attempting to update the plugin code https://pastebin.com/0URjDg5Q to pull from the ACF date fields instead.

Assuming the relevant code is the following (but feel free to look at pastebin for full code):

// Get days with posts
  $dayswithposts = $wpdb->get_results( "SELECT DISTINCT DAYOFMONTH( post_date )
    FROM $wpdb->posts WHERE MONTH( post_date ) = '$thismonth'
    AND YEAR( post_date ) = '$thisyear'
    AND post_type IN ( $post_types ) AND post_status = 'publish'
    AND post_date < '" . current_time( 'mysql' ) . '\'', ARRAY_N );
  if ( $dayswithposts ) {
    foreach ( (array) $dayswithposts as $daywith ) {
      $daywithpost[] = $daywith[0];
    }
  } else {
    $daywithpost = array();

And use this to what I use to get upcoming events based on the ACF date fields:

        <?php
            // Upcoming Events & Events Underday
            $now = date('Y-m-d H:i:s');

            $args = array(
                    'post_type'         => 'events_post_type',
                    'posts_per_page'    => -1,
                    'meta_query'        => array(
                            'relation'      => 'OR',
                            'date_upcoming_clause'   => array(
                                    'key'       => 'event_start_date',
                                    'compare'   => '>=',
                                    'value'     => $now,
                                    'type'      => 'DATETIME'
                            ),
                            array(
                                    'relation'      => 'AND',
                                    'date_started_clause'   => array(
                                            'key'       => 'event_start_date',
                                            'compare'   => '<=',
                                            'value'     => $now,
                                            'type'      => 'DATETIME'
                                    ),
                                    'date_end_clause'   => array(
                                            'key'       => 'event_end_date',
                                            'compare'   => '>=',
                                            'value'     => $now,
                                            'type'      => 'DATETIME'
                                    ),
                            ),
                    ),
                    'orderby' => array(
                            'date_started_clause' => 'ASC',
                            'date_end_clause' => 'ASC',
                            'date_upcoming_clause' => 'ASC',
                    ),
            );

            $the_query = new WP_Query($args);

    ?>

Is there a simple way to have the plugin code utilize the relevant ACF code? ACF is very logical, but unfamiliar with the calendar plugin method which calls the database directly. To note, this use was looking for relatively the same but do not believe found an answer: WORDPRESS: Calendar with custom post type + custom fields. To note, if there is an easier way to do this with the native get_calendar open to that as well.

Author:TechRemarker,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49971507/wordpress-custom-post-type-calendar-with-acf-date-fields
Asher Black :

What I do is modify a custom post type to publish even if it's merely scheduled, and then use the post date as the event date. \n\n/* set to publish even if scheduled and show future date */\n\nremove_action('future_post', '_future_post_hook');\nadd_filter( 'wp_insert_post_data', 'futurenow_do_not_set_posts_to_future' );\n\nfunction futurenow_do_not_set_posts_to_future( $data ) {\nif( ! is_singular( array('events') ) )\nif ( $data['post_status'] == 'future' && $data['post_type'] == 'events' ) \n$data['post_status'] = 'publish';\nreturn $data;\n}\n",
2018-07-31T21:59:41
yy