Support

Account

Forum Replies Created

  • This remains a strange case. Somehow the wp_reset_postdata() isn’t working well. I’m still not sure weather it’s WP or ACF that’s messing up, but either one of them is. Perhaps something to look into.

  • 
    public function pre_populate_teams_dropdown( $field ) {
    	$field['choices'] = array();
    
    	$apollo_home_args = array(
    		'post_type' => 'teams',
    		'nopaging' => true,
    		'fields' => 'ids',
    	);
    
    	$apollo_home_teams = new WP_Query( $apollo_home_args );
    
    	if( $apollo_home_teams->have_posts() ) :
    		while( $apollo_home_teams->have_posts() ) : $apollo_home_teams->the_post();
    			$field['choices']['Apollo'][$post] = get_the_title( $post );
    		endwhile;
    		wp_reset_postdata();
    	endif;
    
    	$apollo_away_args = array(
    		'post_type' => 'opponents',
    		'nopaging' => true,
    		'fields' => 'ids',
    	);
    
    	$apollo_away_teams = new WP_Query( $apollo_away_args );
    
    	if( $apollo_away_teams->have_posts() ) :
    		while( $apollo_away_teams->have_posts() ) : $apollo_away_teams->the_post();
    			$field['choices'][__( 'Opponents', 'apollo' )][$post] = get_the_title( $post );
    		endwhile;
    		wp_reset_postdata();
    	endif;
    
    	return $field;
    }

    Appears to work as well.

  • Hello Elliot,

    I changed it to:

    publicic function pre_populate_teams_dropdown( $field ) {
    	global $post;
    	//var_dump( $post );
    	$field['choices'] = array();
    
    	$apollo_home_args = array(
    		'post_type' => 'teams',
    		'nopaging' => true,
    	);
    
    	$apollo_home_teams = get_posts( $apollo_home_args );
    
    	if( count( $apollo_home_teams ) > 0 ) :
    		foreach( $apollo_home_teams as $team ) {
    			$field['choices']['Apollo'][ $team->ID ] = get_the_title( $team->ID );
    		}
    	endif;
    
    	$apollo_away_args = array(
    		'post_type' => 'opponents',
    		'nopaging' => true,
    	);
    
    	$apollo_away_teams = geT_posts( $apollo_away_args );
    
    	if( count( $apollo_away_teams ) > 0 ) :
    		foreach( $apollo_away_teams as $team ) {
    			$field['choices']['Apollo'][ $team->ID ] = get_the_title( $team->ID );
    		}
    	endif;
    
    	return $field;
    }

    This works, however it strikes me as odd. The function get_posts uses WP_Query too and the wp_reset_postdata() filter makes sure the $post variable is restored. So somehow the $post variable isn’t reset the way it should.

  • Little addendum:

    If I var_dump the $post variable in pre_populate_teams_dropdown I get this:

    object(WP_Post)[8885]
      public 'ID' => int 5084
      public 'post_author' => string '1' (length=1)
      public 'post_date' => string '2014-02-25 10:23:54' (length=19)
      public 'post_date_gmt' => string '0000-00-00 00:00:00' (length=19)
      public 'post_content' => string '' (length=0)
      public 'post_title' => string '' (length=0)
      public 'post_excerpt' => string '' (length=0)
      public 'post_status' => string 'auto-draft' (length=10)
      public 'comment_status' => string 'open' (length=4)
      public 'ping_status' => string 'open' (length=4)
      public 'post_password' => string '' (length=0)
      public 'post_name' => string '' (length=0)
      public 'to_ping' => string '' (length=0)
      public 'pinged' => string '' (length=0)
      public 'post_modified' => string '2014-02-25 10:23:54' (length=19)
      public 'post_modified_gmt' => string '0000-00-00 00:00:00' (length=19)
      public 'post_content_filtered' => string '' (length=0)
      public 'post_parent' => int 0
      public 'guid' => string 'http://loc.apollo8.nl/?post_type=matches&p=5084' (length=47)
      public 'menu_order' => int 0
      public 'post_type' => string 'matches' (length=7)
      public 'post_mime_type' => string '' (length=0)
      public 'comment_count' => string '0' (length=1)
      public 'filter' => string 'raw' (length=3)
    object(WP_Post)[8714]
      public 'ID' => int 4438
      public 'post_author' => string '1' (length=1)
      public 'post_date' => string '2014-02-01 18:01:42' (length=19)
      public 'post_date_gmt' => string '2014-02-01 17:01:42' (length=19)
      public 'post_content' => string '' (length=0)
      public 'post_title' => string 'Heren 1' (length=7)
      public 'post_excerpt' => string '' (length=0)
      public 'post_status' => string 'publish' (length=7)
      public 'comment_status' => string 'open' (length=4)
      public 'ping_status' => string 'open' (length=4)
      public 'post_password' => string '' (length=0)
      public 'post_name' => string 'heren-1' (length=7)
      public 'to_ping' => string '' (length=0)
      public 'pinged' => string '' (length=0)
      public 'post_modified' => string '2014-02-01 18:01:42' (length=19)
      public 'post_modified_gmt' => string '2014-02-01 17:01:42' (length=19)
      public 'post_content_filtered' => string '' (length=0)
      public 'post_parent' => int 0
      public 'guid' => string 'http://loc.apollo8.nl/teams/heren-1/' (length=36)
      public 'menu_order' => int 0
      public 'post_type' => string 'teams' (length=5)
      public 'post_mime_type' => string '' (length=0)
      public 'comment_count' => string '0' (length=1)
      public 'filter' => string 'raw' (length=3)

    So the first time it’s ok, but the second round it changes somehow.

    And if I disable one of the dropdowns, it also seems to work ok. So this problem only occurs if both dropdowns are being pre_filled.

  • Ah right. Mine will be filled programmaticly. So if I understand correctly, a multi-dimensional array will do the trick. something like

    array(
      'Optgroup' => array( 'choice1', 'choice2' );
    )

    etc.

  • Hm, ok, if I do this programmaticly I can already use the optgroup. In that case, it can already do what I need it to 🙂

  • Cool, thanks. I’ll try something 🙂

  • Ok, cool.

    Can I use this code in a new plugin? I see it extends the ACF class, so this works ‘stand-alone’?

  • Is this something we can expect to see in future versions of the plugin? The optgroup is something that I want to use too. Fiddeling in the plugin code isn’t really an option, since an update of the plugin will nullify the changes made.

  • Hello Elliot,

    My apologies for my late response.

    I found some more issues with ACF extensions. For instance the repeater date/time picker. If I use a custom date_format and time_format sometimes the values that are filled in are not being stored in the DB. It counts the field, the number of fields that are filled in is upped in the DB, just not the value that was chosen.

    Somehow I am experiencing a lot of issues, I think I might need to try all this on a clean WP install and see what happens. I will do so asap and see what happens.

  • Hello Elliot,

    sure enough those conditions can all be true.

    If the post is of post type course AND it's a child AND it's a child of the post with a value stored in $this->options['webinar_parent']. Come to think of it, I can remove the second statement, but it still works.

    The fields all do show up on the right places, that all works nicely. I do always make the fields in the UI and then export it to PHP. Works like a charm.

    The value from the field is also stored in the DB. No problems there.

    The example above about the webinar code was a poor one, since that one does not show up on an options page.

    
    if(function_exists("register_field_group")) {
    	register_field_group(array (
    		'id' => 'acf_webinar-settings',
    		'title' => __( 'Webinar settings', 'ictrecht' ),
    		'fields' => array (
    			array (
    				'key' => 'field_5266959515e47',
    				'label' => __( 'Parent', 'ictrecht' ),
    				'name' => 'webinar_parent',
    				'type' => 'select',
    				'choices' => array (
    				),
    				'default_value' => '',
    				'allow_null' => 1,
    				'multiple' => 0,
    			),
    			array (
    				'key' => 'field_5266967c15e4a',
    				'label' => __( 'Price', 'ictrecht' ),
    				'name' => 'webinar_price',
    				'type' => 'text',
    				'default_value' => '',
    				'placeholder' => '',
    				'prepend' => '€',
    				'append' => '',
    				'formatting' => 'html',
    				'maxlength' => '',
    			),
    			array (
    				'key' => 'field_5266973215e4b',
    				'label' => __( 'Price for lawyers', 'ictrecht' ),
    				'name' => 'webinar_lawyer_price',
    				'type' => 'text',
    				'default_value' => '',
    				'placeholder' => '',
    				'prepend' => '€',
    				'append' => '',
    				'formatting' => 'html',
    				'maxlength' => '',
    			),
    			array (
    				'key' => 'field_5266975c15e4c',
    				'label' => __( 'Registration form', 'ictrecht' ),
    				'name' => 'webinar_registration_form',
    				'type' => 'select',
    				'choices' => array (
    				),
    				'default_value' => '',
    				'allow_null' => 1,
    				'multiple' => 0,
    			),
    		),
    		'location' => array (
    			array (
    				array (
    					'param' => 'options_page',
    					'operator' => '==',
    					'value' => 'courses_settings',
    					'order_no' => 0,
    					'group_no' => 0,
    				),
    			),
    		),
    		'options' => array (
    			'position' => 'normal',
    			'layout' => 'default',
    			'hide_on_screen' => array (
    			),
    		),
    		'menu_order' => 0,
    	));
    }

    This is a good example.

    If I now on a single page want to use webinar_price, I try to do this:

    echo get_field( 'webinar_price', 'option' );
    Or
    echo get_field( 'webinar_price', 'options' ); // Should do the same, but tried this one too

    But it doesn’t work. If I var dump the return value from get_field I get a false in both cases.

  • That is just the code I used here to describe the problem. My actual code looks like this:

    
    if(function_exists("register_field_group")) {
    	register_field_group(array (
    		'id' => 'acf_webinar',
    		'title' => 'Webinar',
    		'fields' => array (
    			array (
    				'key' => 'field_525fffb950527',
    				'label' => 'Webinar code',
    				'name' => 'webinar_code',
    				'type' => 'text',
    				'default_value' => '',
    				'placeholder' => '',
    				'prepend' => '',
    				'append' => '',
    				'formatting' => 'html',
    				'maxlength' => '',
    			),
    		),
    		'location' => array (
    			array (
    				array (
    					'param' => 'post_type',
    					'operator' => '==',
    					'value' => 'course',
    					'order_no' => 0,
    					'group_no' => 0,
    				),
    				array (
    					'param' => 'page_type',
    					'operator' => '==',
    					'value' => 'child',
    					'order_no' => 1,
    					'group_no' => 0,
    				),
    				array (
    					'param' => 'page_parent',
    					'operator' => '==',
    					'value' => $this->options['webinar_parent'],
    					'order_no' => 2,
    					'group_no' => 0,
    				),
    			),
    		),
    		'options' => array (
    			'position' => 'side',
    			'layout' => 'default',
    			'hide_on_screen' => array (
    			),
    		),
    		'menu_order' => 0,
    	));
    }

    Now, when I use either get_field( ‘webinar_code’ ) or the_field( ‘webinar_code’ ), it returns false even though a value is stored.

    The get_field() function doesn’t work anywhere, I tried it in several files.

  • I have tried the updated version from GIT. It works fine, thank you.

    However, the get_field() bug, still remainds. A value stored from an options page, can not be retrieved by using the_field or get_field. Those functions still return false.

  • Hello Elliot,

    thanks for your reply.

    I worked around the first issue by not picking a custom slug but using the normal one that is generated from the options page name. Then I can create custom fields on the options page.

    When I do that and save those fields, they do show up in the database, but get_field() returns false.

    Thank you for the update, I will try it out and get back on it and let you know if it works.

  • Hey Elliot,

    I can select the suboptions page in the interface, but fields are just not shown. Also not if I do this via code.

    For the field issue, I use this code to create a field:

    
    array (
    	'key' => 'field_5266967c15e4a',
    	'label' => 'Field',
    	'name' => 'field-name',
    	'type' => 'text',
    	'default_value' => '',
    	'placeholder' => '',
    	'prepend' => '',
    	'append' => '',
    	'formatting' => 'html',
    	'maxlength' => '',
    ),
    

    And I use this code to get the field value

    
    get_field( 'field-name', 'options' );
    

    But when I dump the result, I get a false.

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