Support

Account

Forum Replies Created

  • @jonas that function is awesome! You are awesome. Thank you!

  • Doh! The alt displays by default BUT you need to have an ALT entered on the image (I had a caption and no alt text, so the alt was not showing). I thought the markup would still be there but empty alt="" but if no alt there is not alt markup output.

  • Thank you. I marked this as the solution before seeing the answer from @hube2. His solution made my code work (as I did not know about the false parameter, and the added benefit of not needing ACF to do additional queries) but your solution worked as well.

  • Ah, interesting. I didn’t know about unformulated values and the false parameter. Thank you.

    For anyone seeing/needing this later, here is the final working code (just remove 'option', if you’re not using a gallery from an Options page):

    <?php 
    	$images = get_field('gallery', 'option', false);
    	$size = 'full'; // (thumbnail, medium, large, full or custom size)
    	$rand = array_rand($images, 1);
    
    	if( $images ): ?>
    		<?php echo wp_get_attachment_image( $images[$rand], $size ); ?>
    <?php endif; ?>
  • Yes, sorry, perhaps I should have been more clear. I’m not sure how to make the $rand variable apply. I tried things like this, to no avail:

    
    <?php 
    	$images = get_field('gallery', 'option');
    	$size = 'full'; // (thumbnail, medium, large, full or custom size)
    	$rand = array_rand($images, 1);
    
    	if( $images ): ?>
    	       	<?php echo wp_get_attachment_image( $images[$rand], $size ); ?>
    <?php endif; ?>
    
  • Unfortunately I cannot help. Not much of a PHP expert myself 🙁 Glad I was able to help with the first part, though.

  • @edumusa my apologies for not responding all that time ago 🙁 Perhaps this will help you as well (though, I doubt you still need it)


    @fuberator
    – here is my code, using Custom Post Type “event”. It’s my original post code combined with Elliot’s answer (with a few changes, I think…it’s been awhile). I hope it helps:

    <?php              
    
                $currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
    
                    /* Order Posts based on Date Picker value */
                    $posts = get_posts(array(
                        'posts_per_page' => -1,
                        'post_type' => 'event', // name of custom post type
                        'meta_key' => 'event_enddate', // name of custom field
                        'orderby' => 'meta_value_num',
                        'order' => 'DESC',
                        'meta_query'=> array(
                            array(
                              'key' => 'event_enddate',
                              'compare' => '<',
                              'value' => $currentdate,
                              'type' => 'DATE',
                        ))
                    ));
    
                    $years = array();
    
                    if( $posts )
                    {
                        foreach( $posts as $post )
                        {
                            setup_postdata( $post );
                            
                            // get date
                            $date = date_create( get_field('event_enddate') );
                            
                            // get year
                            $year = date_format($date,'Y');
                            
                            // create new year if not already exists
                            if( !isset( $years[ $year ]) )
                            {
                                $years[ $year ] = array(
                                    'title' => $year,
                                    'posts' => array()
                                );
                            }
                            
                            // add post to year
                            $years[ $year ]['posts'][] = $post;
                            
                        }
                        wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
                    }
    
                    if( $years )
                    {
                        foreach( $years as $year )
                        {
                            echo '<h2>' . $year['title'] . '</h2>';
                            echo '<ul>';
                            if( $year['posts'] )
                            {
                                foreach( $year['posts'] as $post )
                                {
                                    setup_postdata( $post );
                                    
                                    // get date
                                    $date = date_create( get_field('event_enddate') );
                            
                                    echo '<li><a href="';
                                    the_permalink();
                                    echo '">';
                                    the_title();
                                    echo '</a><br>';
                                    echo date_format($date,'F d, Y');
                                    echo '</li>';
                                }
                            }
                            echo '</ul>';
                        }
                        
                        wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
                    }
             ?>
  • Thank you @folosophy for posting the code (and the fix w/ the counter). I’m wondering if you would be able to help with a solution for pre-populating the repeater with post titles from a custom post type instead of values from another ACF repeater?

    I have a Custom Post Type “Teams” and each post’s title is a team name. I would like to pre-populate an ACF Repeater Field with each team name. Any suggestions?

  • I found this newer post about pre-populating repeaters: https://support.advancedcustomfields.com/forums/topic/pre-populated-repeater-field/

    But couldn’t get it to work 🙁 But then found this solution that uses an Options Page: https://support.advancedcustomfields.com/forums/topic/dynamically-populating-repeater-fields-with-default-values/

    Hope this helps someone.

  • Desperately trying to get this to work. I thought I understood it but I am getting no pre-populated fields at all. My repeater field name is “games” with 4 sub-fields. The first sub-field name is “team” and is a text field. I’m trying to pre-populate the team field. Am I missing something?:

      function my_acf_load_value($value, $post_id, $field) {
        if ($value !== NULL) {
          // if the value is exactly NULL it means
          // the field has never been updated
          // we don't want to change fields that have already been editied
          return $value;
        }
        // set the new field value
        $value = array(
          // add a nested array for each row
          array(
            // add an array item for each sub field
            'team' => 'hello world?',
            'team_1' => 'hello world?', // with or without "_1, etc."?
          )
        );   return $value;
      }
      add_filter('acf/load_value/name=games', 'my_acf_load_value', 10, 3);
  • Any chance this has been implemented and I’m just missing where? I’ve needed this functionality a few times now (a repeater field with 3 or 4 default items, ie: # of Bedrooms, # of Bathrooms, Sq.Ft) and then more fields are required but not always the same ones (ie: Yard Size, Patio Size, Strata Fees).

    All field types would be the same (simple text fields).

  • Right on! Thank you. I would not have figured that out 🙁 Looks to be working with your code. I really appreciate it!

  • Doh! Looks like in troubleshooting I had actually changed the return value from “ID” to “URL”. All is working now (with moving the code as you suggested, which I had tried before, but after having changed the return value and forgetting).

  • Thanks for the quick reply, Will. I should have mentioned that I tried that to no avail. And also I do have the slide_photo image field set to Image ID, not URL or Object.

  • P.S. – I purchased the Repeater add-on today. Very nice. It will be very helpful. Was hoping to only need the ACF Plugin for this project but looks like something else/another plugin will need to be implemented for the Events section.

  • Ugh! I didn’t see that you removed the post type query bits when you provided the debug code, so the proper post type was not being queried:

    	/* Order Posts based on Date Picker value */
    	$posts = get_posts(array(
    		'post_type' => 'event', // name of custom post type
    		'meta_key' => 'event_enddate', // name of custom field
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC'
    	));

    But I still only get <pre> </pre> output.

    I give up. 6+ days on & off trying to figure out what seems like it should be a fairly basic feature. This project needs to move forward 🙁

    I just want the date output as “Month Day, Year” and a Past Events page to show only events with an end date that has passed and an Upcoming Events page to show only events with a future date.

  • Thanks Elliot. When I paste that code the page only outputs <pre> </pre>, nothing else. So I would assume that means the code dies at the first part, so there is a problem with the line: $date = get_field('event_enddate');

    But I don’t know what 🙁 I can use get_field(‘event_enddate’) fine on any other page. Any ideas?

  • Okay, I have the posts showing now, in order of date (ascending). But the date is always “January 1 1970”. My save format is the ACF default yymmdd and my display format is the ACF default: dd/mm/yy

    Where would the January 1 1970 be coming from?

    <?php get_header(); ?>
    
    	<?php 
    	/*
    	*  Create PHP DateTime object from Date Piker Value
    	*  The ACF "Workaround" for PHP 5.2 as shown in the docs
    	*/
    	$date = get_field('event_enddate'); // $date = 19881123 (23/11/1988)
    	 
    	// extract Y,M,D
    	$y = substr($date, 0, 4);
    	$m = substr($date, 4, 2);
    	$d = substr($date, 6, 2);
    	 
    	// create UNIX
    	$time = strtotime("{$d}-{$m}-{$y}");
    	 
    	/* Order Posts based on Date Picker value */
    	$posts = get_posts(array(
    		'post_type' => 'event', // name of custom post type
    		'meta_key' => 'event_enddate', // name of custom field
    		'orderby' => 'meta_value_num',
    		'order' => 'ASC'
    	));
    	 
    	if( $posts )
    	{
    		foreach( $posts as $post )
    		{
    			setup_postdata( $post );
    	 
    			the_title();
    			echo date('F n Y', $time);
    
    		}
    		wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    	}
    ?>
          
    <?php get_footer(); ?>
  • I had searched the forums for “filter by date picker” and only found this post: (http://support.advancedcustomfields.com/forums/topic/filtering-cpt-with-date-picker-custom-field/) that seemed like it might be somewhat helpful at first but I cannot make sense of it.

    I did try the example code on the Date Picker Field Documentation page, as a starting point, but I was getting this error on the events page: Fatal error: Call to undefined method DateTime::createfromformat()

    After much hair pulling I see that I mistakenly thought I was running PHP 5.3.24 (on my hosting) but today remembered this particular site is on different hosting (the client’s hosting) and their server is running PHP 5.2.6.

    So now I’ve attempted this code (from the Date Picker docs page, referring to PHP versions less than 2.3) and all it outputs is “01/01/1970January 1 1970”. 🙁

    <?php get_header(); ?>
    
    <?php 
    /*
    *  Create PHP DateTime object from Date Piker Value
    *  The ACF "Workaround" for PHP 5.2 as shown in the docs
    */
    $date = get_field('event_end-date'); // $date = 19881123 (23/11/1988)
     
    // extract Y,M,D
    $y = substr($date, 0, 4);
    $m = substr($date, 4, 2);
    $d = substr($date, 6, 2);
     
    // create UNIX
    $time = strtotime("{$d}-{$m}-{$y}");
     
    // format date (23/11/1988)
    echo date('d/m/Y', $time);
     
    // format date (November 11th 1988)
    echo date('F n Y', $time);
     
    /* Order Posts based on Date Picker value */
    $posts = get_posts(array(
    	'post_type' => 'event', // name of custom post type
    	'meta_key' => 'event_end-date', // name of custom field
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC'
    ));
     
    if( $posts )
    {
    	foreach( $posts as $post )
    	{
    		setup_postdata( $post );
     
    		echo 'hello?';
    
    	}
    	wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    }
      
    ?>
    <?php get_footer(); ?>
    
Viewing 21 posts - 1 through 21 (of 21 total)