Home Forums General Issues has_sub_field won't work after POST request


has_sub_field won't work after POST request

  • Hello guys,

    I stumbled upon a strange issue that took already too much time.

    I have a custom post type with many custom fields and I fetch them with the standard ACF functions. I also have a button which sends a form via POST request. Now if you click this button and the page reloads, the has_sub_field loop suddenly doesn’t work anymore. However, get_field works perfectly. This is really strange to me and I haven no solution for this. Do I miss something? I provide you some code:

    function pdf_button(){
        global $post;
        $id = $post->ID;
        echo get_field( 'description_titel', $id);
        while(has_sub_field('schritte', $id)):
            echo ("Schritt<br>");
        return '<form method="post" id="fdpf-form">
                    <input type="hidden" name="post_id" value="'.$id.'" />
                    <button class="button button-primary" 
                        value="generate">Als PDF herunterladen

    This code is added via shortcode. You see, there is a button. If I click this button , the page reloads and the while-loop just doesn’t appear.
    What can I do?

  • The first thing I would do is check the global $post ID to make sure that’s what you expect it to be. You can error_log( print_r( $post, 1 ) ); if you have error logging turned on. That would be the only thing I can thing of that would be messing up actually getting the field content.

  • Thanks for the reply!

    The $post and its ID are actually fine, I checked them. Otherwise the get_field function wouldn’t work as well.

    So at first everything works, you can see the echoed ‘description_titel’ and then a few times ‘Schritt’ because of the has_sub_field-loop.

    But if you press the button and the page reloads, you only see the echoed ‘description_titel’ which means that get_field works but has_sub_field doesn’t. And that really confuses me.

    Let’s say we can’t make it work this way, which other possibilities do I have to get the desired fields?

    P.S Maybe it is a version conflict?
    ACF version: 5.6.8
    WP version: 4.9.8

  • Alright, I managed to solve the problem by using the native get_post_meta() function.

    The reason I needed this post request is a button in a form that prints a PDF of my custom post type. And after you press the button, a POST request is fired. Maybe there are better solutions but I sticked for it and didn’t want to change it.

    First, I found out how many subfields there are:

    $stepcount = get_post_meta($id, 'schritte')[0];

    Then, I itereated:

        for ($i = 0; $i < $stepcount; $i++) {
            echo ($i+1).". ".get_post_meta($id, 'schritte_'.$i.'_schritt_beschreibung')[0]."<br>";

    The most important part is the field name (schritte) and the subfields $i__schritt_beschreibung. schritt_beschreibung is the slug of the subfield.

    I hope it helps

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

The topic ‘has_sub_field won't work after POST request’ is closed to new replies.