Support

Account

Home Forums Front-end Issues Only select particular custom fields

Solved

Only select particular custom fields

  • I have created a Group Field called “Document File”, and within this group created only one file type field ‘file’ to upload files called “zip_file”. I have added at the top of a template file

    acf_form_head();

    and calls the form custom field by

    acf_form( 
        array( 
            'post_id' => $post_id, 
            'form' => false, 
        ) 
    ); 
    

    As you can see, I am not loading the form, so assigned the variable ‘form’ => false. But I wanted to use all the upload feature provided by WordPress Media Upload Screen so I don’t have to create all the features myself and it works great because only “zip_file” custom field is displayed.

    I am saving this field value by hooking to:

    function doc_save_post($post_ID) {
        if ( isset($_POST['zip_file']) ) {
            update_field('field_51ad3464c3055', $_POST['zip_file'], $post_ID); 
        }
    }
    add_action('save_post', 'doc_save_post');
    

    So far so good. The problem I am having is with creating a new post. For some reason the “zip_file” isn’t loaded anymore, instead the default WordPress post textarea editor is loaded even if the code is

    acf_form( 
        array( 
            'post_id' => $post_id, 
            'form' => false, 
        ) 
    ); 
    

    So wanted to know how to retrieve only the single “zip_file” custom field, without loading anything else? Strangely, the “zip_file” custom field isn’t loaded in this case, only the textarea post field is.

    Was following the tutor from
    http://www.advancedcustomfields.com/resources/tutorials/using-acf_form-to-create-a-new-post/

    Thank you.

  • Hi @acfbigfan

    Can you elaborate more on this ‘text area’ field? Is this field part of teh same field group of zip_file? If so, can you look at the source code to see if the zip_file field is created?

    If the ‘text area’ field is part of another field group, then for some reason, ACF is finding the wrong field group for this post_id parameter.

    May I ask what the $post_id value is?
    If it is not a real post, you will have to specify the field group IDs manually like in the tutorial. This is missing from your code.

    Thanks
    E

  • Thank you for getting back to me Elliot. Regarding to your questions:

    Can you elaborate more on this ‘text area’ field? Is this field part of teh same field group of zip_file? If so, can you look at the source code to see if the zip_file field is created?

    No, “zip_file” is the only field created.

    If the ‘text area’ field is part of another field group, then for some reason, ACF is finding the wrong field group for this post_id parameter.

    There are no other field group. I probably have to look deeper into this.

    May I ask what the $post_id value is?

    I am having problem only with new post, to display this custom field before even able to submit the form. I used ‘new’ for $post_id, so I can handle it later like the tutor.

    If it is not a real post, you will have to specify the field group IDs manually like in the tutorial. This is missing from your code.

    Okay, I will try that. About group IDs, are you referring to this part of the code?

    $args = array(
        'post_id' => 'new',
        'field_groups' => array( 123 )
    );

    If yes, the 123 is the field group ID number from the URL when we edit the field group

    post=123

    correct? Thank you once again!

  • Hi @acfbigfan

    Yep, 123 is the ID of the field group which you can see in the URL when you edit the field group.

    I can’t explain why a mystery textarea input is being created, this makes no sense. Perhaps you have some other fields defined in your theme or another plugin?

    Thanks
    E

  • Thank you Elliot. Assigning the Field Group ID solved the issue.

    Just a question, is there a way to assign the uploaded file to particular POST or uploaded user? So only that user have privilege to delete or select the uploaded file to the post? And to restrict that file to appear to particular e posts, when media file manager is opened?

    Initially I thought the “Library -> Uploaded to post” was for that kind of feature, but I was wrong.

    Thank you once again for your fantastic support.

  • Hi @acfbigfan

    Sorry, can you re-explain that last comment? I’m a bit confused.

    Thanks
    E

  • For example, I am a user A. I have added a file called car.zip to a post called “Beautiful car”.

    Another user called B, also going to upload a file for post called “Wonderful car”, but from the Media Manager screen, he can see “car.zip” uploaded by user A and also select it, although it can not be deleted.

    So I was looking for a way to tie the uploaded file to that single post only, or to a particular user only (the owner that uploaded the file) so it won’t be visible from another post or by another user.

    Thanks!

  • Hi @acfbigfan

    Thanks for clarifying.

    The image fiedl contains an option for ‘library’ and you can limit it to only show images uploaded to the current post.

    This would normally work, but because you are using ‘new’ as the post_id, I don’t know if this setting will work as desired…

    Perhaps you will need to use a more basic image uploader. I have created such a field and you can find a beta version of it attached to this comment:

    Thanks
    E

  • Thank you so much! Will try that out.

  • I’ve tried acf-basic-file plugin using with acf_form. Was able to select the file and upload it (it seems), and using get_field() retrieves the file name. Using print_r(get_field_object(‘field_890afd659f789’)) displays the following:

    Array
    (
        [key] => field_890afd659f789
        [label] => the_file
        [name] => the_file
        [type] => basic_file
        [order_no] => 1
        [instructions] => 
        [required] => 0
        [id] => acf-field-the_file
        [class] => basic_file
        [conditional_logic] => Array
            (
                [status] => 0
                [rules] => Array
                    (
                        [0] => Array
                            (
                                [field] => null
                                [operator] => ==
                            )
    
                    )
    
                [allorany] => all
            )
    
        [max_file_size] => 
        [field_group] => 30
        [value] => the_file.zip
    )
    

    But the file is nowhere to be found from the server? Not in WordPress media list or using FTP client.

    Thanks!

  • Decided to use the default “file” instead, looks like the simple field still has bug. But would prefer the simpler version if stable version is released. Thank you!

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

The topic ‘Only select particular custom fields’ is closed to new replies.