Support

Account

Forum Replies Created

  • Hi @britta , please, can you post your complete solution to @b1naryb0y original question ? I can’t understand what you made to get working.

    Me, I had to take an alternative approach.

    The following code will never work for repeater fields:

    $args = array (
    		    'post_type' => 'event',
    		    'meta_query' => array(
    			    'relation'		=> 'AND',
    				array(
    			        'key'		=> 'event_dates_%_start_date',
    			        'compare'	=> '<=',
    			        'value'		=> '20150524',
    			    ),
    			     array(
    			        'key'		=> 'event_dates_%_end_date',
    			        'compare'	=> '>=',
    			        'value'		=> '20150524',
    			    )
    		    ),
    		);

    As suggested by @mediawerk , event_dates_0_start_date AND event_dates_1_end_date accomplish the start before testdate and end after testdate.

    So I replaced it with:

    array(  
                  'key' => 'opening_dates',
                  'value' => 20150524,
                  'compare' => 'LIKE'
                 ),
    			

    The 'opening_dates' postmeta field contains all the single dates resulting from the repeating “start_date"/"end_date" couple and it was created with this code of mine placed in functions.php using the "acf/save_post" hook:

    
    	function create_opening_dates_array_when_saving_post_with_repeating_start_end_dates($post_id) {
    	
    	
    		// bail early the post type to apply
    		// src: https://support.advancedcustomfields.com/forums/topic/acfsave_post-action/
    		$post_type = get_post_type($post_id);
    			if ($post_type != 'event' ) {
    				return;
    			}
    			
    			
    			// not used here
    			$fields = $_POST['acf']; 
    			
    			// $intervals will contain the interval couples: ['single_interval']=> array("20170301", "20170320")
    			$intervals = array();
    			
    			
    			// 
    			// loop repeater fields
    			if( have_rows('event_dates') ):
    			
    			 	// loop through the rows of data
    			    while ( have_rows('event_dates') ) : the_row();
    			
    			       // for each row, get the "from" "to" couple and save it in the "single_interval" key (array) in the intervals array
    			       $from =  get_sub_field('start_date', false, false);
    			       $to =  get_sub_field('end_date', false, false);
    			       $intervals[]['single_interval'] = array($from, $to);
    
    			
    			    endwhile;
    			
    			else :
    			
    			    // no rows found
    			
    			endif;
    			 
    		// here will be saved the opening date vales	
    		$final_array = array();
    
    		 // loop the array containing the couples of intervals
    		 foreach($intervals as $single_interval) {
    			 
    			 // $intervals = array("20170301", "20170320")
    			 foreach($single_interval as $intervals) {
    			 	
    			 	// fill in missing in-between dates...
    				$arrays_of_single_dates = getDatesFromRange($intervals[0], $intervals[1]);
    				
    				// loop the resulting array and save each single value in the final array...
    				foreach($arrays_of_single_dates as $single) {
    					$final_array[] = intval($single); // from string to integer
    				}
    			}
    		
    		};
    	  // var_dump($final_array);
    	  
    	  
    	  // create or update a meta_key field called "opening_dates" and set as meta_value the $final_array containing all the opening dates
    	  update_post_meta( $post_id, 'opening_dates', $final_array);
    
    	 // var_dump($final_array);	  
    	};
    
    	
    	add_action('acf/save_post', 'create_opening_dates_array_when_saving_post_with_repeating_start_end_dates', 1);
    

    I’ve got the getDatesFromRange function from here.

    I wonder if @Elliot knows a simpler and better performing solution anyway…

  • Hi @matekk , contrary to what is said in https://www.advancedcustomfields.com/resources/get-values-from-an-options-page/, is it possibile with the ACF Api.

    We create for example a “Page A” in this way:

    $page_a = array(
                    'page_title' => 'Page A',
                    'menu_title' => 'Page A',
    		'post_id' => 'page_a'
    		);
    acf_add_options_page($page_a);
    
    

    And a “Option” page in this similar way:

    $option = array(
    		'page_title' => 'Option',
                    'menu_title' => 'Option',
    		'post_id' => 'option'
    		);
    acf_add_options_page($option);
    

    Now you create a new Field Group with an “images” field and make this field visible when “Page Options” = “Page A” and similarly you create a new Field Group with an “images” field (it can also be a clone of the previous “images” field but watch out that the clone field name is something like “clone-images”) and make this field visible when “Page Options” = “Option”.

    Now you can get the “images” field from “Page A” page with:

    the field("images", "page_a")

    and the “images” field from “Option” page with:

    the field("images", "option")

    because in Wp DB (‘wp_options’ page) the “images” field from “Page A” is saved as option_name ‘page_a_images” and the “images” field from “Option” page is saved as “option_images”.

  • Hi mean the ‘HTML tab’, no the ‘Visual’ one, sorry I have not expressed well, thanks!

  • Use the appropriate hook

    functions.php

    
    function my_admin_enqueue_scripts() {
    
    	wp_enqueue_script( 'date-picker-js', get_template_directory_uri() . '/js/custom_date_picker.js', array(), '1.0.0', true );
    
    }
    
    add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');
    

    custom_date_picker.js

    
    // function to merge two Javavascipt objects IE compatible, from prototype.js http://prototypejs.org/doc/latest/language/Object/extend/
    function extend(destination, source) {
    	for (var property in source)
    		destination[property] = source[property];
    	return destination;
    }
    
    // Acf hook -> Hooks -> Filters -> date_picker_args
    // see https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/
    acf.add_filter('date_picker_args', function( args, $field ){
    	
    	// do something to args
    	
    	var custom_args = {
    	  yearRange:			"-200:+100", // value to change
    	};
    	
    	args = extend(args, custom_args)
    	
    	// return
    	return args;
    			
    });
    
  • Hi,

    in Acf 5.5.6 (I use the Pro Version), you will need to edit assets/js/acf-input.js file at line 4736 and 4764 and 4953:

    from:

    
                            // create options
    			var args = { 
    				dateFormat:			this.o.date_format,
    				altField:			this.$hidden,
    				altFormat:			'yymmdd',
    				changeYear:			true,
    				yearRange:			"-100:+100", // value to change
    				changeMonth:		true,
    				showButtonPanel:	true,
    				firstDay:			this.o.first_day
    			};

    to:

    
                            // create options
    			var args = { 
    				dateFormat:			this.o.date_format,
    				altField:			this.$hidden,
    				altFormat:			'yymmdd',
    				changeYear:			true,
    				yearRange:			"-200:+100", // value changed!
    				changeMonth:		true,
    				showButtonPanel:	true,
    				firstDay:			this.o.first_day
    			};

    Then you have to minify input.js in input.min.js because this is the file that is read in production; you can use an online tool like https://javascript-minifier.com.

    Finally clear any browser cache to get the new range.

    Or – more simply and cleaner:

    Use the appropriate hook

    functions.php

    
    function my_admin_enqueue_scripts() {
    
    	wp_enqueue_script( 'date-picker-js', get_template_directory_uri() . '/js/custom_date_picker.js', array(), '1.0.0', true );
    
    }
    
    add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');
    

    custom_date_picker.js

    
    // function to merge two Javavascipt objects IE compatible, from prototype.js http://prototypejs.org/doc/latest/language/Object/extend/
    function extend(destination, source) {
    	for (var property in source)
    		destination[property] = source[property];
    	return destination;
    }
    
    // Acf hook -> Hooks -> Filters -> date_picker_args
    // see https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/
    acf.add_filter('date_picker_args', function( args, $field ){
    	
    	// do something to args
    	
    	var custom_args = {
    	  yearRange:			"-200:+100", // value to change
    	};
    	
    	args = extend(args, custom_args)
    	
    	// return
    	return args;
    			
    });
    
  • Hi!

    With the plugin Really Simple Csv Import, that is free and open source (https://github.com/hissy/rs-csv-importer), you can import Csv fields like “name a, name b, name c” as repeater fields or <select> elements. The author made specific hooks (filters) for this goal and they are well documented here.

    For example, taking advantage of the “really_simple_csv_importer_save_meta filter” I managed to import, from a Csv, a field written like “author a, author b, author c” as a ACF repeater in WP, with the following code of mine:

    add_filter('really_simple_csv_importer_save_meta', function($meta, $post, $is_update) {
         if (strpos($value, ',') !== false) { // if there is a "," in the string...
    	            $_value = preg_split("/,+/", $value); // "author a, author b, author c" pattern is converted in an array
    	            $rows = count($_value); // return the number of the elements in array
    	            $meta[$key] = $rows; // in ACF scheme, this is the meta_key for the number of rows, ie: "meta_key : authors, meta_value: 2"
                
                	// the loop creates each single row, in the form required by ACF: "meta_key: authors_0_author, meta_value: Mario Rossi" 
    	            for ($i=0; $i<$rows; $i++) {
    		            $plural = 'authors';
    		            $addition_sign = '_';
    		            $singular = 'author';
    		            
    		            $meta[$plural.$addition_sign.$i.$addition_sign.$singular] = $_value[$i]; // ie:  $meta['authors_0_author'] = $_value[0];
    	            } // end "for"
    
    } // end if
    
    else { // when the repeater has only one value...
    	        	 $i = 0;
    	        	
    	        	 $plural = 'authors';
    		         $addition_sign = '_';
    		         $singular = 'author';
    
    	        	 $meta[$key] = 1;
    	        	 $meta[$plural.$addition_sign.$i.$addition_sign.$singular] = $value;
            	} // end else
    
    });
  • Oooooops!

    Sorry, this is an EPIC FAIL from me, I always forget that <p> aren’t nestable….

    Thanks!

  • Hi Elliot!

    The developer of the plugin told me to insert the custom field created with Acf in the save_post function of the plugin. Here is the code, can you help me?

    function save_post( $post_id, $post ) {
    		global $wpdb, $ai1ec_events_helper, $ai1ec_importer_plugin_helper;
    
    		// verify this came from the our screen and with proper authorization,
    		// because save_post can be triggered at other times
    		if( isset( $_POST[AI1EC_POST_TYPE] ) && ! wp_verify_nonce( $_POST[AI1EC_POST_TYPE], 'ai1ec' ) ) {
    			return;
    		} else if( ! isset( $_POST[AI1EC_POST_TYPE] ) ) {
    			return;
    		}
    
    		if( isset( $post->post_status ) && $post->post_status == 'auto-draft' ) {
    			return;
    		}
    
    		// verify if this is not inline-editing
    		if( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'inline-save' ) {
    			return;
    		}
    
    		// verify that the post_type is that of an event
    		if( isset( $_POST['post_type'] ) && $_POST['post_type'] != AI1EC_POST_TYPE ) {
    			return;
    		}
    
    		// LABEL:magicquotes
    		// remove WordPress <code>magical</code> slashes - we work around it ourselves
    		$_POST = stripslashes_deep( $_POST );
    
    		$all_day          = isset( $_POST['ai1ec_all_day_event'] )    ? 1                                             : 0;
    		$instant_event    = isset( $_POST['ai1ec_instant_event'] )    ? 1                                             : 0;
    		$start_time       = isset( $_POST['ai1ec_start_time'] )       ? $_POST['ai1ec_start_time']                    : '';
    		$end_time         = isset( $_POST['ai1ec_end_time'] )         ? $_POST['ai1ec_end_time']                      : '';
    		$venue            = isset( $_POST['ai1ec_venue'] )            ? $_POST['ai1ec_venue']                         : '';
    		$address          = isset( $_POST['ai1ec_address'] )          ? $_POST['ai1ec_address']                       : '';
    		
    		$compilatore            = isset( $_POST['ai1ec_event_1_nome_e_cognome_compilatore'] )            ? $_POST['ai1ec_event_1_nome_e_cognome_compilatore']                         : '';
    		
    		$city             = isset( $_POST['ai1ec_city'] )             ? $_POST['ai1ec_city']                          : '';
    		$province         = isset( $_POST['ai1ec_province'] )         ? $_POST['ai1ec_province']                      : '';
    		$postal_code      = isset( $_POST['ai1ec_postal_code'] )      ? $_POST['ai1ec_postal_code']                   : '';
    		$country          = isset( $_POST['ai1ec_country'] )          ? $_POST['ai1ec_country']                       : '';
    		$google_map       = isset( $_POST['ai1ec_google_map'] )       ? 1                                             : 0;
    		$cost             = isset( $_POST['ai1ec_cost'] )             ? $_POST['ai1ec_cost']                          : '';
    		$is_free          = isset( $_POST['ai1ec_is_free'] )          ? (bool)$_POST['ai1ec_is_free']                 : false;
    		$ticket_url       = isset( $_POST['ai1ec_ticket_url'] )       ? $_POST['ai1ec_ticket_url']                    : '';
    		$contact_name     = isset( $_POST['ai1ec_contact_name'] )     ? $_POST['ai1ec_contact_name']                  : '';
    		$contact_phone    = isset( $_POST['ai1ec_contact_phone'] )    ? $_POST['ai1ec_contact_phone']                 : '';
    		$contact_email    = isset( $_POST['ai1ec_contact_email'] )    ? $_POST['ai1ec_contact_email']                 : '';
    		$contact_url      = isset( $_POST['ai1ec_contact_url'] )      ? $_POST['ai1ec_contact_url']                   : '';
    		$show_coordinates = isset( $_POST['ai1ec_input_coordinates'] )? 1                                             : 0;
    		$longitude        = isset( $_POST['ai1ec_longitude'] )        ? $_POST['ai1ec_longitude']                     : '';
    		$latitude         = isset( $_POST['ai1ec_latitude'] )         ? $_POST['ai1ec_latitude']                      : '';
    		$post_twitter     = isset( $_POST['ai1ec_oauth_provider_twitter'] )
    			? (bool)$_POST['ai1ec_oauth_provider_twitter']
    			: false;
    
    		$rrule  = NULL;
    		$exrule = NULL;
    		$exdate = NULL;
    
    		// if rrule is set, convert it from local to UTC time
    		if( isset( $_POST['ai1ec_repeat'] ) && ! empty( $_POST['ai1ec_repeat'] ) )
    			$rrule = $ai1ec_events_helper->ics_rule_to_gmt( $_POST['ai1ec_rrule'] );
    
    		// if exrule is set, convert it from local to UTC time
    		if (
    			isset( $_POST['ai1ec_exclude'] ) &&
    			! empty( $_POST['ai1ec_exclude'] ) &&
    			NULL !== $rrule // no point for exclusion, if repetition is not set
    		) {
    			$exrule = $this->_merge_exrule(
    				$_POST['ai1ec_exrule'],
    				$_POST['ai1ec_rrule']
    			);
    			$exrule = $ai1ec_events_helper->ics_rule_to_gmt( $exrule );
    		}
    
    		// if exdate is set, convert it from local to UTC time
    		if( isset( $_POST['ai1ec_exdate'] ) && ! empty( $_POST['ai1ec_exdate'] ) )
    			$exdate = $ai1ec_events_helper->exception_dates_to_gmt( $_POST['ai1ec_exdate'] );
    
    		$is_new = false;
    		$event 	= null;
    		try {
    			$event = new Ai1ec_Event( $post_id ? $post_id : null );
    		} catch( Ai1ec_Event_Not_Found $e ) {
    			// Post exists, but event data hasn't been saved yet. Create new event
    			// object.
    			$is_new = true;
    			$event = new Ai1ec_Event();
    			$event->post_id = $post_id;
    		}
    		// If the events is marked as instant, make it last 30 minutes
    		if( $instant_event ) {
    			$end_time = $start_time + 1800;
    		}
    
    		$event->start               = $ai1ec_events_helper->local_to_gmt( $start_time );
    		$event->end                 = $ai1ec_events_helper->local_to_gmt( $end_time );
    		$event->allday              = $all_day;
    		$event->instant_event       = $instant_event;
    		$event->venue               = $venue;
    		
    		$event->compilatore         = $compilatore;
    		
    		$event->address             = $address;
    		$event->city                = $city;
    		$event->province            = $province;
    		$event->postal_code         = $postal_code;
    		$event->country             = $country;
    		$event->show_map            = $google_map;
    		$event->cost                = $cost;
    		$event->is_free             = $is_free;
    		$event->ticket_url          = $ticket_url;
    		$event->contact_name        = $contact_name;
    		$event->contact_phone       = $contact_phone;
    		$event->contact_email       = $contact_email;
    		$event->contact_url         = $contact_url;
    		$event->recurrence_rules    = $rrule;
    		$event->exception_rules     = $exrule;
    		$event->exception_dates     = $exdate;
    		$event->show_coordinates    = $show_coordinates;
    		$event->longitude           = trim( $longitude ) !== '' ? (float) $longitude : NULL;
    		$event->latitude            = trim( $latitude ) !== '' ? (float) $latitude : NULL;
    
    		// if we are not saving a draft, give the event to the plugins. Also do not pass events that are imported from facebook
    		if( $post->post_status !== 'draft' && $event->facebook_status !== Ai1ecFacebookConnectorPlugin::FB_IMPORTED_EVENT ) {
    			$ai1ec_importer_plugin_helper->handle_post_event( $event, 'save' );
    		}
    		$saved_post_id = $event->save( ! $is_new );
    		if ( $post_twitter ) {
    			if ( ! add_post_meta(
    					$saved_post_id,
    					'_ai1ec_post_twitter',
    					'pending',
    					true
    			 ) ) {
    				update_post_meta(
    					$saved_post_id,
    					'_ai1ec_post_twitter',
    					'pending'
    				);
    			}
    		}
    
    		$ai1ec_events_helper->delete_event_cache( $post_id );
    		$ai1ec_events_helper->cache_event( $event );
    		// LABEL:magicquotes
    		// restore <code>magic</code> WordPress quotes to maintain compatibility
    		$_POST = add_magic_quotes( $_POST );
    		return $event;
    	}
    
  • Hi Elliot!

    Yes, yesterday I’ve contacted the developers of the plugin and I’m in wait of an answer.

    However, I thought that – to pass a value in a input box to update a custom field – it would be enough to use your APIs.

    It is only important to me that the text the user inserts will appear (in the backend) in the custom field I’ve created with ACF in event post.

    Let me know,

    Thanks!

    Davide

  • Hi!

    I’ve tried also with:

    	<?php 
     
    $field_key = "field_5265167430efd";
    $value = htmlentities($_GET['compilatore']); 
    update_field( $field_key, $value )?>
    	
    	<div class="row-fluid">
    		<input type="text" id="ai1ec_compilatore" name="compilatore"
    			
    			class="span12" />
    	</div>
    

    but it doesn’t work 🙁

Viewing 11 posts - 1 through 11 (of 11 total)