0
votes

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.

2
It's disturbing that you were downvoted with no explanations. Have an upvote. I'm also looking for a calendar to display CPTs based on an ACF date that is unrelated to post date. I hope you found a solution.MTAdmin

2 Answers

1
votes

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.

/* set to publish even if scheduled and show future date */

remove_action('future_post', '_future_post_hook');
add_filter( 'wp_insert_post_data', 'futurenow_do_not_set_posts_to_future' );

function futurenow_do_not_set_posts_to_future( $data ) {
if( ! is_singular( array('events') ) )
if ( $data['post_status'] == 'future' && $data['post_type'] == 'events' ) 
$data['post_status'] = 'publish';
return $data;
}
0
votes

I don't know why you used these
date_upcoming_clause,date_start_clause,'date_end_clause fields just use directly custom field(meta_key and meta_value) like event_start_date and event_end_date and join the postmeta table to posts table you will get the exact result you want.

`"SELECT * "
. "FROM $wpdb->posts wpposts";
  $sql1 .= "JOIN $wpdb->postmeta pm ON (wpposts.ID = pm.post_id)
 WHERE post_type = 'your_custom_post_type' AND post_status = 'publish'
 AND pm.meta_key = 'event_start_date'
 AND pm.meta_value = 20180423
 OR pm.meta_key ='event_end_date'
AND pm.meta_value=20180425
";`