Support

Account

Forum Replies Created

  • I would say that you’ll need to go about setting the default value differently in this case. You’re using a custom taxonomy, so that should make things a bit less difficult since your not dealing with “Uncategorized”

    Rather than using acf/load_field in this case you should use acf/load_value https://www.advancedcustomfields.com/resources/acfload_value/ with a priority > 10

    Going to be honest, I’m not exactly sure what needs to be done, this is a bit of a guess.

    
    add_filter('acf/load_value/key=field_58086a9b13be5', 'kiteboat_set_tax_default', 20, 3);
    
    function kiteboat_set_tax_default( $value, $post_id, $field ) {
    	if ($value === false && get_post_status($post_id) == 'auto-draft') {
    		$value = 304;
    	}
      return $value;
    }
    
  • I think I recreated it. Do you have Load Terms set to yes?

  • Are you trying to add a default value to a field on a post that was previously save without a value?

    If this is an exiting post, does it do the same thing when you add a new post?

    What version of ACF are you using?

  • This has been reported to the developer.

  • K, I get it now. Sometimes it takes me a while but I’ll get there.

    Would have helped me to understand if you’d said something like, I can add content before but I can’t add it after. You may have understood what you meant, but I did not. I read it as “I can’t add content using acf/render_field. I can only add content for specific field types.”

  • Yes, I did. I have many fields and field groups on a test site. I added the code exactly as I posted it to the functions.php file on the test site and that content <p>This is a Test</p> appeared before every field on the site.

  • Yes, you would need to create a unique filter for each field. Of you can make the decision base on the field.

    You can run your filter on every field, or a specific field as explained in the document I linked to above.

    ACF passes the current field values to your function in $field

    
    function set_my_field_label($field) {
    

    This will have the field name, field key, and all the other information about the field. If you want to see what you have to work with add

    
    echo '<pre>'; print_r($field); echo '</pre>';
    

    to the top of your filter, I think this is also explained in the documentation. This will output all of the field values. You can then base what field you get from the options page on one of the field values, like the name.

  • Put it in your functions.php file

    
    // this adds a filter for the field that you want
    // to dynamically set the label for
    // it can be used in several ways
    // https://www.advancedcustomfields.com/resources/acfload_field/
    add_filter('acf/load_field/key=field_0123456789abc', 'set_my_field_label');
    
    // this is the function that is called for the filter
    function set_my_field_label($field) {
      // get the value from the field you want to use for the label
      $label = get_field('field_label_option', 'option');
      if ($label) {
        // if the option is set then change the field label
        // almost any field argument can be changed dyamically
        $field['label'] = $label;
      }
      return $field;
    }
    
  • Whenever this happens, 99% of the time is has to do with a pre_get_posts filter filtering something it shouldn’t. All of the examples that you’ll find, or at least most of them that I’ve seen, do not take into account that the filter is run on every query done in WP and the examples to not include information on making sure you don’t apply the filter to things that you should not. The reason that the examples don’t is because, well, that information is not easy to come by and takes a bit of trial and error.

    This is where the PHP error log comes in handy.

    
    function mine_add_custom_types( $query ) {
      ob_start();
      print_r($query);
      error_log(ob_get_clean());
    }
    add_filter( 'pre_get_posts', 'mine_add_custom_types' );
    

    This will put every query done when loading a page into the PHP (WP) error log. You can then examine the error logs and figure out how to target only the query you want to alter.

    Although, in the case of the search results and adding post types is to make sure you only apply your filter to “The Main Query”

    
    function mine_add_custom_types( $query ) {
      if (!$query->is_main_query()) {
        return;
      }
      // the rest of your code
    }
    add_filter( 'pre_get_posts', 'mine_add_custom_types' );
    
  • I just tried this and the example I gave still seems to be working.

    This if for checkbox/multiselect field type

    
    add_filter('acf/load_field/key=field_589c6d5477f05', 'set_tax_default');
    function set_tax_default($field) {
    	$field['default_value'] = array(3,4);
    	return $field;
    }
    

    and this works for radio and single select

    
    add_filter('acf/load_field/key=field_589c6d5477f05', 'set_tax_default');
    function set_tax_default($field) {
    	$field['default_value'] = 3;
    	return $field;
    }
    

    The value needs to match the term ID(s) of the default values you want to set.

  • What types of fields are you unable to add content to?

    I just tried this with many types of fields and the text is displayed before every field I tried it on.

    
    add_action('acf/render_field', 'test_render_field', 1);
    function test_render_field($field) {
    	?><p>This is a Test</p><?php 
    }
    
  • Set the default label when you create the field

    
    add_filter('acf/load_field/key=field_0123456789abc', 'set_my_field_label');
    function set_my_field_label($field) {
      $label = get_field('field_label_option', 'option');
      if ($label) {
        $field['label'] = $label;
      }
      return $field;
    }
    
  • Do each of the forms form tags, meaning they are separate forms or are they all included inside a single form. By default each acf_form() is a separate form and must be submitted separately unless you’ve set the ‘form’ argument to false. If you have not done this then the fields of one form will not be submitted with another form.

    If you do have them all in one forms, then there isn’t any way to tell which fields go with a certain form except to check each field key. If each form is a separate field group then the “parent” of each top level field object will be the field group key. But there isn’t any way to trigger a different filter based on this information that I know of.

    Not sure if any of this will help you or not.

  • acf/render_field is run on all field types and then specific field types

    in /api/api-field.php on line 381.

    
    // create field specific html
    do_action( "acf/render_field", $field );
    do_action( "acf/render_field/type={$field['type']}", $field );
    

    No, ACF does not use the filter, it does the actions

  • the_sub_field() as with the_field() echos the value of a field rather than return it. You need to use get_sub_field() which returns a values so that you can do something with it.

  • I don’t see where this $files[] = get_attached_file( $singlefile['ID'] ); would be any different. It gets the attachments file location path from the ID of the file. As long as your returning either the File ID, or File Array so that you can get the ID, there should be no difference from using image fields except that gathering the list of files will be more complicated as I said above.

  • acf/save_post is run on any page that has acf fields for either adding or updating a post. It is dependable, but like I said, is not called when deleting a post.

  • the developer knows about these things and there’s really nothing that he can do about it. The purchase from Elliot comes with automatic updates, in fact, that’s the only reason to purchase it from here. I don’t see how this other place can provide automatic updates unless they are altering the code. If they’re not providing automatic updates then you’re really paying for nothing.

  • acf/pre_save_post only runs on the front end of the site for acf_form().

    The best hook to run after ACF saves data is acf/save_post with a priority > 10.

    But this will not work when deleting posts. For deleting posts you’ll need to rely on some WP hook, but I don’t know what that would be.

  • I’ve tested your export and it imported correctly, including the choices of the select field.

    Are you using the most recent update for ACF? I have 5.5.6

    Have you tried deactivating other plugins to see if that helps? maybe there is a conflict.

  • Can you upload the export? You’ll need to put it in a .zip first.

  • I could be wrong, but this is my understanding of things.

    When you’re in the admin on a post edit page and you click on “insert image” in the editor, or add images using an ACF field, these images are “attached” the current post that is being editor. WP has a post ID to associate the images with at the time they are inserted.

    If you select an image that is already attached to another post, this image does not get “attached” to the post you are editing. What post an image is “attached” to is set by the post_parent row in the database and a post (attachment) cannot have 2 parents.

    If you go to the media library and upload images, unattached images will be attached to the first post they are used on.

    When creating a new post in the admin, WP creates an “auto draft” of the new post and uses the id of this yet to be created post for attaching images.

    On a front end form if you are editing and existing post, in other words, the post ID used for acf_form() is the same as the post ID where the form is displayed, WP will also attach any image that are uploaded or added to the fields that are not already attached.

    In the case of adding a new post using an acf_form() there is no auto draft post created. When the images are uploaded WP does not know where to attach them to. It could be that they get attached to the post ID of the current post being shown or to nothing at all. I haven’t actually investigated this. There could be other cases, for example if your ACF form is shown on some type of archive page where there is not “post id”. It could be that the images are not attached anywhere, or possibly they are attached to the last post in the last loop. I also don’t know what happens when you’re editing a different post in ACF than the post where the form appears. All of these things will act in one way or another to confuse WP as to exactly where it should be attaching new images or adding images images that were not previously attached. The only thing that I am sure of is that if the image is already “attached” to another post.

    How can this be corrected?

    Add an update value filter to image and gallery fields. Check to the images to see if they are attached to the current post ID being saved. In order to keep with the way WP does things you should also make sure that they are not attached to another post. If they are not attached to something else then attach them to the current post by setting their post_parent value. But this will not help in the case where WP confuses where to attach them in the first place since it’s already attached them. In this case you would need to break from the way WP usually does things and always attach the image to the current post being edited.

    I don’t honestly see an acceptable way to correct this since WP does not keep any type of list anywhere telling you every location where an image is used and it does not have the ability to attach and image to more than one post….. unless I’m missing something.

  • Is this exactly where and the order that the code appears on you page? or are you just leaving out the code in between. https://www.advancedcustomfields.com/resources/acf_form/

    acf_form_head(); must be placed before any HTML is generated. Generally before get_header(); in your template or in your header.php file something like this

    
    <?php
    /**
     * The template for displaying the header
     *
     * Displays all of the head element and everything up until the "site-content" div.
     *
     * @package WordPress
     * @subpackage Twenty_Fifteen
     * @since Twenty Fifteen 1.0
     */
    
     acf_form_head();
    
    ?><!DOCTYPE html>
    

    It may be saved in the database, but is it save to a post_id with a post type of “dog”?

  • There isn’t any tool that I am aware of that will let you export and import values from/to the wp_options table. Anything like this you’d probably be looking at building yourself.

    Keep in mind that for every option name you have that there is another value in the table that matches it with the field_key value associated with the field. Option names created by ACF are "options_{$your_field_name}" and the matching field key is stored under "_options_{$your_field_name}". Repeaters or anything with sub fields gets a lot more complicated.’

    When dealing with options, or any value that may or may not be present, for example if these options are part of a theme and the option values are needed when the theme is installed, the best choice, and best coding practice, is to have a default value for all fields, use the default value in your code and assume that the value may not exist. For example

    
    $value = get_field('my_option_field', 'options');
    if (!$value) {
      $value = 'my default value'
    }
    

    This can also be written something like this

    
    if (($value = get_field('my_option_field')) === NULL) {
      $value = 'my default value';
    }
    

    Yes, this is a little more work but much less work than building and export/import tool for these values.

Viewing 25 posts - 9,701 through 9,725 (of 14,467 total)