Support

Account

Forum Replies Created

  • In my case, I just wanted to complete empty my repeater field so I tried “delete_field($field_key, $post_id);” because I am constructing an array and i am using add_row to put the array into the field. My setup got a little complicated with repeater inside of repeater inside of repeater. Seemed the best approach was construct an array and use add_row.

    The delete_field seemed to do the trick, but is that a good way to completely remove all values from a repeater? That is exactly what I wanted it to do. Tested locally and appears to work.

    All other methods like delete_row() didn’t seem to work, it was always left with something in the repeater. I tried looping through and deleting rows, still same issue, it always left something behind.

  • @hube2 I am working in a very locked down system.

    Thank you for your help and suggestions. We did consider at some point putting all of the json for the fields groups in our theme, but because our site is already existing and we didn’t do that from the beginning we felt it to be too risky at this point.

    For this thing i am making, i will export the fields and import them into my other environments. I still want to be able to get the field_keys programmatically so i don’t have to hard code all of those values.

    Thanks!

  • This does go to show that developers need a function to get the field keys.

    I propose something like: get_field_key( $field_group, $field_name ) ?

    This way you can just give it the field group id: group_123456789

    And the name of the field in the group.

    The function would return the field key. Noone wants to hard code the field keys into the code because when you create the fields in another environment, the field keys can change on you.

  • BTW, I will have the post id because i am doing wp_insert_post which returns the newly created post.

    The data I am currently using for testing with acf_maybe_get_field is data that was already imported, but not imported using the field_key. It was just using the wp_insert_post and the field names. This data when I use get_fields() returns null.

    In my new importer, I’m going to do like the example shows on the link you provided. I need to test this, but i was thinking something like this:

    function helper_get_field_key($field_name, $post_id){
      $maybe_field_obj = acf_maybe_get_field( $field_name, $post_id, false );
    
      return $maybe_field_obj['key'];      
    }
    
    // Create new post.
    $post_data = array(
        'post_title'    => 'My post',
        'post_type'     => 'my-post-type',
        'post_status'   => 'publish'
    );
    $post_id = wp_insert_post( $post_data );
    
    // Save a basic text value.
    $get_field_key_email = helper_get_field_key('email', $post_id);
    $value = "[email protected]";
    update_field( $get_field_key_email, $value, $post_id );

    I think something like this will work, i haven’t tested this exact scenario yet. I will today though and i can report back.

  • @hube2 This is what i was going to do…but are you saying don’t use acf_maybe_get_field()? With my broken data, it seems to work well. I can get the field_key by passing it the name of the field and the post id. I was going to do something simple like this.

    function helper_get_field_key($field_name, $post_id){
      $maybe_field_obj = acf_maybe_get_field( $field_name, $post_id, false );
    
      return $maybe_field_obj['key'];      
    }
    $get_field_key_email = helper_get_field_key('email', $post_id);
    print_r($get_field_key_email);

    Returns: field_5f401336ed966

    This output the field key for the email field that is in that post id.

    If there is a better way to get the field_key programmatically when no value exists yet, I’m all ears…Would love to have a function to use just to get the field key.

  • @hube2 What function should I use to get the field key for this scenario? I can pass the field name and the post id to whatever function to get the field key. I am building a new custom importer, so i need a way to get the field key before any value has been stored into the field. I thought acf_maybe_get_field would be the ticket. It seems to work great.

    I have some data that suffers from not having the reference data in the database. However when i edit the post, i see all of the fields filled in on the edit post screen. I know the data is there, but the data was imported via field name not field key.

    I want to rectify this error in my new importer.

    This data was imported using a custom script. I did some experimentation. I had tried function get_fields(), get_field_object(), get_field_objects() – none of these worked to get field data. They all returned empty.

    I do not want to hard code the field keys into my importer because the field keys will change from environment to environment. I work in a 4 environment. local, test, staging, and prod. Just looking for a way to get the field keys programmatically. That’s why i was going to use acf_maybe_get_field to get the field key and then save the data using wp_insert_post and run a script that also update_fields using the field keys during the import process.

    What function should i use to get the field keys?

  • Thank you John.

    I found a helper function called acf_maybe_get_field() – my plan is to use this function to get the field keys of each field.

    I will use wp_insert_post to create the post.

    Then after successful post creation, I will use update_field with the field key to update the post field values. This should save the data into wordpress as if i had manually added it in the wp-admin.

    From what i have found acf_maybe_get_field() is the only function that can get me the field keys even before the values exist.

    Does this sound like a solid plan?

  • I like the helper function here, but what if I am not registering the field groups?

    is there another similar function to get the key value of the field?

    I use wp_insert_post to import data into wordpress, should i use a different function. I have the same issue as you. get_fields() is empty until i manually save my post.

    I don’t want to run into this issue anymore. Let me know your suggestions.

    I may answered my own question here. I believe I can write a function where I pass it the name of the field and then use this get_field_objects to get the key. Thoughts?

  • can you provide the link you are using for your endpoint?

  • At the moment, I have changed it. I made my function into a shortcode.

    So now in the single.php I am doing echo do shortcode.

    I’m getting the same result as before.

    Before, I was just calling the function.

    echo get_views_counter()

    I’ll do a more thorough code review to see if something is coming in late and hitting the page.

  • Also, i checked the view page source and there is only one <html> tag.

  • This is just a weird problem.

    A couple of bits of information here…I’ve been doing some digging.

    I installed WP DEBUG and I am only seeing in the Queries tab the value being updated once. I know that it is running twice though. I think the UPDATE sql statement in the logs only once.

    Our site does use AJAX, but it is not firing on page load and that is literally all I am doing is just loading the page. When I refresh, I can see the number go up by 2. Like I explained before, if i open up my database and query the post i am on, if i keep refreshing on that query, I will see it update the number twice.

    I have read some blogs that are saying that single.php can fire twice before of revisions or something so i added this to functions file.

    I also tried moving my function from the functions file to the single.php template. The number still updated twice.

    I even tried converting the function into a shortcode and then instead of calling the function directly, I did a do_shortcode in single.php, but it incremented the number twice.

    I’ve done site wide searches to see if the function is called anywhere else and it is now only in this one template. The function returns the counter to the page and it only outputs once, but that UPDATE statement seems to fire twice.

    I’ll keep playing around with it.

  • I seem to be having the same kind of issue. I have a function in my functions.php for my theme.

    The function looks like this:

    function get_views_counter() {
    	global $post;
    
    	$post_id_for_counter = $post->ID;
    	$current_view_counter = (int) get_field('view_counter', $post_id_for_counter);
    
    	if($current_view_counter >= 0) {		
    		$updated_counter = $current_view_counter + 1;
    		update_field('view_counter', $updated_counter, $post_id_for_counter);
    	}
    	
    	$new_view_counter = (int) get_field('view_counter', $post_id_for_counter);
    	$number_formated_int = (int) number_format($new_view_counter);
    
    	$output = $new_view_counter . " views";
    
    	return $output;
    
    }

    The way I am testing it is I have the database open and i do a query so i can see the value of the view_counter field right in the database. I refresh that query while I am loading the page.

    I go to the browser, open an article from the site where this function is fired, and if i echo something out from this function I only see it output once.

    Initially when the page is loaded, I see in the database the number will increment once. Then after a few seconds, it increments again. If I sit there and hit refresh I will see it increment by 2 every time.

    I had read of some issues with single.php loading twice. I am in single.php so trying to figure what it causing my function to fire twice.

    Could it be that it is in the functions file?

  • That is probably close enough. Thanks!

  • Ideally what I would like is a field type with a search box that searches tags. The options i see right now that are available only lists the tags as checkboxes or other…

    I’d like to have a search box with autocomplete. I dont want users to be able to add tags when they edit a post (they can go to the tag manager in wordpress for that).

    Does this field type exist?

  • In Beaver Themer is a little wrench icon near the social icons when you go to edit them? Can you just remove the icons that you don’t have links for?

    To answer your question, you would add the code example i gave where the icons output in your template, but i am no familiar with beaver themer so i dont know if that is possible in your case.

  • You mean you do not want it to show on the front-end correct?

    You can achieve by using:

    $facebook_link = get_field(‘facebook_link’);
    if($facebook_link) {
    echo $facebook_link;
    }

  • great, thank you. I was able to get this to work.

  • That would give me the posts that are related to “clients”, but thats not what i am trying to do.

    I’m trying to show the client names in a drop down menu if that client has been related to a post. Right now, i am just showing all of the client names, but then some of the client names don’t have anything related to them. So, when a user would select the client name from the drop down menu it won’t show any results. I want to filter those unrelated clients out from the list.

    Anyway to do that?

  • hi clay, thanks for your feedback. The way i fixed my issue was i optimized my code and re-organized my data. I moved all of my Flexible Custom Fields into individual posts in a custom post type and setup a new “relation” field to move those modules into the page. Sped things up.

  • Did you ever solve this issue? I am trying to write a filter on my own custom field and not having much luck. Can you post your solution if you have one?

  • Try this, just before the_title, try to echo the $post->ID. At least then you will know if you are getting the correct post or if there is a disconnect between the two.

    You can also try this:

    $ID = $post->ID;
    echo get_the_title( $ID );

    You can also try this, this is code from my page where i use

    $posts = get_sub_field('carousel_project'); // relationship field
    if( $posts ): ?>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
    <?php setup_postdata($post); ?>
    
    <?php if( have_rows( 'repeater_multimedia' ) ): ?> // repeater field inside of the related post
    <?php while( have_rows( 'repeater_multimedia' ) ): the_row(); ?>

    Maybe if you change your top part to this, you might have better luck.

  • i tried this myself on the FCF and it wouldnt create the initial group of empty fields. This works with the repeater field. So when i need a group of fields i’m usually using that.

  • I looks like when we use $posts = get_field(‘relationship_field’);

    and then use foreach ($posts as $post):

    This causes issues with the $posts variable. I know i was not able to reset it.

    So, i saw this thread on your board.

    http://support.advancedcustomfields.com/forums/topic/displaying-a-relationship-field-on-a-custom-post-type-taxonomy-page/

    And i modified my code to match what her solution ended up being:

    And now i have this:

    <?php
    $post_id = 19;
    
    $headerqry = array( 
    	'p' => $post_id,
    	'post_type' => 'any',
    	'posts_per_page' => 1,
    	'status' => 'published'
    );
    
    $loop = new WP_Query( $headerqry );
    
    if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
    
    $promo_posts = (array) get_field('header_promo_banner');
    
    if( $promo_posts ): ?>
        <?php foreach( $promo_posts as $promo_post): // variable must be called $post (IMPORTANT) ?>
            <?php 
    					$attachment_id = get_field('promo_image', $promo_post->ID);
    					$size = "promo-header";
    					$image_attributes = wp_get_attachment_image_src( $attachment_id, $size );
    				
    				if( $image_attributes ) {
    				?>
            	<a href="<?php echo the_field('promo_url', $promo_post->ID); ?>" target="_blank"><img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"></a>
        <?php } endforeach; ?>
    <?php endif; ?>
    <?php endwhile; endif; wp_reset_query(); ?>

    And this is letting single.php using standard wordpress loop run. So, I would say that there is definitely a problem with the Relationship field when you use the $posts variable.

    Can you please look into this?

  • Actually that did not solve the problem. It seems anytime i put a relationship field in the header, page, sidebar, anywhere – anytime there is a wordpress default loop after that, that loop won’t run.

    If i comment out this code in my header-news.php file, then the rest of the page will run just fine.

    I have tried variations of this code too, but it doesnt seem to work.

    Maybe if you can take a look you can spot something wrong with it.

    <?php
    $post_id = 19;
    
    $headerqry = array( 
    	'p' => $post_id,
    	'post_type' => 'any',
    	'posts_per_page' => 1,
    	'status' => 'published'
    );
    
    $loop = new WP_Query( $headerqry );
    
    if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
    
    $posts = get_field('header_promo_banner');
    
    if( $posts ): ?>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <?php 
    					$attachment_id = get_field('promo_image');
    					$size = "promo-header";
    					$image_attributes = wp_get_attachment_image_src( $attachment_id, $size );
    				
    				if( $image_attributes ) {
    				?>
            	<a href="<?php echo the_field('promo_url'); ?>" target="_blank"><img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"></a>
        <?php } endforeach; ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    <?php endwhile; endif; wp_reset_query(); ?>

    On most pages i am using global $post; $post_id = $post->ID; to get the ID of the current page outside of the loop.

    I was hoping that wp_reset_query would reset the loop and let the items after this query run, but it doesnt seem to work.

    Then the code in my single-10.php file is pretty basic:

    while ( have_posts() ) : the_post(); ?>
    
    	<div class="main-photo-wrapper">
      	<img src="<?php echo the_field('header_photo'); ?>" style="width:100%; height:auto;" alt="<?php echo the_field('header_alt_text'); ?>"/>
    	</div>
      
      <div class="title-bar">
      	<h2><?php the_title(); ?></h2>
      </div>
      
      <div class="article-content">
      <?php the_content(); ?>
      </div>
      
    <?php endwhile;  // end of the loop. ?>
Viewing 25 posts - 1 through 25 (of 41 total)