Viewing 14 replies - 1 through 14 (of 14 total)
  • Thread Starter DiZzYZA

    (@dizzyza)

    For those who need a fix this is how i solved it.

    //These are the values i got from the calendar
    $cmb_date_start = '2016-07-01';
    $cmb_date_end   = '2016-07-30';
    
     $args = array(
        'posts_per_page' => -1,
        'post_type' => 'product',
        'meta_query' => array(
        'relation' => 'AND', // This is default, you could skip it
        array(
            'key' => '_test_text_date_timestamp',
    	'compare' => '<=',
    	'value' => strtotime($cmb_date_end),
        ),
        array(
    	'key' => '_test_text_date_timestamp',
    	'compare' => '>=', // Same as above
    	'value' => strtotime($cmb_date_start),
    	),
        ),
    );

    Add the custom args above to just before this line

    $the_query = new WP_Query( $args );

    Then i changed the following to stop it pulling from the actual post date

    $post_date = substr($post->post_date, 0, 10);
    $post_timestamp = strtotime($post->post_date);

    to this

    $post_date = get_post_meta( $post->ID, '_test_text_date_timestamp', true );
    $post_timestamp = date("Y-m-d", strtotime($post_date));

    And lastly i changed this

    $item = array ("title" => $title, "color" => $color, "start" => date('Y-m-d\TH:i:s', $post_date), "end" => date('Y-m-d\TH:i:s', $post_date), "url" => get_permalink($post->ID), 'post_id' => $post->ID );

    to this

    $item = array ("title" => $title, "color" => $color, "start" => date('Y-m-d\TH:i:s, $post_date), "end" => date('Y-m-d\TH:i:s, $post_date), "url" => get_permalink($post->ID), 'post_id' => $post->ID );

    Hope this helps anyone looking ??

    Hi, I know this may have worked for you, but I’m trying to do something similar, I’ve got a WooCommerce store that ‘sells’ courses which have dates assigned to them. The dates are added to a custom field in the product page and I need this date to be the one on the calendar, NOT the post date. I’ve tried your solution but now nothing shows on the calendar – the only thing I’ve changed is the name of the meta field to mine – but I don’t understand where the $cmb_date_end and $cmb_date_start comes from? Or is it hard coded to the month you want to display (seems a bit silly?)

    Thanks

    Thread Starter DiZzYZA

    (@dizzyza)

    Hey memerson

    I used the $cmb_date_end and $cmb_date_start in my example above to show what kind of date format the calendar was looking for.
    as my date is using unix time

    Can you paste the code you do have then we can have a look?
    Not sure why

    $post_date = get_post_meta( $post->ID, 'Your-Custom-Field', true );

    doesn’t work for you

    Thanks for the response.

    I’m using:

    $course_date = get_post_meta( $post->ID, '_course_date', true );
     $date_bits = explode("/",$course_date);
     $post_date = $date_bits[2] . '-' . $date_bits[1] .'-'. $date_bits[0];
     $post_timestamp = date("Y-m-d", strtotime($post_date));

    The meta _course_date comes out in the format 25/08/2016 (d/m/Y) which is why I use the explode to reformat the date to be able to process the timestamp.

    I don’t have a start and end date, just a single date – does this mean I need to refactor the $args to work correctly?

    Thread Starter DiZzYZA

    (@dizzyza)

    No Worries

    Trying to replicate your setup and changing this worked for me

    $item = array ("title" => $title, "color" => $color, "start" => date('Y-m-d', $course_date), "end" => date('Y-m-d', $course_date), "url" => get_permalink($post->ID), 'post_id' => $post->ID );

    Also make sure this is commented out, coz it will then force to look for post date

    //$where .= $wpdb->prepare(" AND post_date >= %s AND post_date < %s", $_REQUEST['start'], $_REQUEST['end']);

    Holding thumbs!

    Thanks for the help.

    I did realise why something went wrong as the format of the course date in the database was not correct, so I changed it to the format yyyy-mm-dd and cut out the explode. I also cleaned up some of the other code and finally got the data out but it still does not show on the calendar.

    Here is the jSON returned from the query:

    [{"title":"Plant Supports","color":"#a8d144","start":"2016-08-06","end":"2016-08-06","url":"http:\/\/courses2.musgrovewillows.co.uk.172-24-16-212.mo-server6.co.uk\/product\/plant-supports-aug\/","post_id":100}]

    My $item array line is now:

    $item = array (“title” => $title, “color” => $color, “start” => $post_date, “end” => $post_date, “url” => get_permalink($post->ID), ‘post_id’ => $post->ID );

    (as you can see I have eliminated the date() formatting since it’s the correct format to begin with). I’ve commented out the $where as suggested and my new args are:

    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'product',
        'meta_query' => array(
            array(
                'key' => '_course_date',
                'compare' => '<=',
                'value' => $_REQUEST['end'],
            ),
            array(
                'key' => '_course_date',
                'compare' => '>=', // Same as above
                'value' => $_REQUEST['start'],
            ),
        ),
    );

    So, basically, even though it retrieves the data, it does not display on the calendar – I’m sure I’m missing something really simple but I can’t see it…

    EDIT:

    Got it working!

    Changed this line:

    $item = array ("title" => $title, "color" => $color, "start" => $post_date, "end" => $post_date, "url" => get_permalink($post->ID), 'post_id' => $post->ID );

    to this:

    $item = array ("title" => $title, "color" => $color, "start" => date('Y-m-d\TH:i:s', strtotime($post_date)), "end" => date('Y-m-d\TH:i:s', strtotime($post_date)), "url" => get_permalink($post->ID), 'post_id' => $post->ID );

    Forgot the fact that Full Calendar does require the ‘Y-m-d\TH:i:s’ format to work!

    Thanks for all the advice ??

    Thread Starter DiZzYZA

    (@dizzyza)

    Glad you got it sorted!

    Hi, I’ve tried to work with ACF fields but doesn’t work.

    When I change this:

    	    	$post_date = substr($post->post_date, 0, 10);
    	    	$post_timestamp = strtotime($post->post_date);

    to

    		$time_date = get_field('event_date_time');
    		$time_date_end = get_field('event_date_time_end');
    		
    		$time_date_end += date_create($time_date_end);
    		date_add($time_date_end, date_interval_create_from_date_string('+1 day'));
    		$post_date = substr($time_date, 0, 10);		
    
    		$post_timestamp = strtotime($time_date);
    				
    		$end_timestamp = strtotime($time_date_end);

    All work only at first load. If I change month it doesn’t load events.

    Thanks for help

    This is my page

    • This reply was modified 8 years, 3 months ago by lupet92.

    EDIT: if I add an event in old months work, if I add a event in next month doesn’t work.

    Thread Starter DiZzYZA

    (@dizzyza)

    Just looked at your page. I see events in both this month and December. Did you manage to resolve?

    Resolved

    comment this line:

    //$where .= $wpdb->prepare(" AND post_date >= %s AND post_date < %s", $_REQUEST['start'], $_REQUEST['end']);

    Thread Starter DiZzYZA

    (@dizzyza)

    I see

    //$where .= $wpdb->prepare(" AND post_date >= %s AND post_date
     < %s", $_REQUEST['start'], $_REQUEST['end']);
    

    was listed in my replies above.
    Glad you got it sorted!

    • This reply was modified 8 years, 3 months ago by DiZzYZA.
    • This reply was modified 8 years, 3 months ago by DiZzYZA.

    Thanks for quick reply!
    EDIT: sorry I did not see answer.

    • This reply was modified 8 years, 3 months ago by lupet92.
Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘WP FullCalendar Using CMB2 Date Field’ is closed to new replies.