Support

Account

Home Forums ACF PRO Can I have a repeater field with NO rows

Solved

Can I have a repeater field with NO rows

  • I am struggling with doing something that should be very simple but I can’t figure it out.

    I have a repeater field that *can* be used on every post, but is optional. However if it is NOT used, there is still an entry in the database that reflects 1 row (because it appears on the post edit screen?).

    I don’t know how to make it NOT show a row and just the “add row” button so that if it isn’t used, then there isn’t any value in the database, or no entry at all in the database for that repeater field.

    Here’s why:
    I want to display a section below the post with a header that says “Additional Resources” – if no rows are entered, then no header and no section.

    However all of my “if” tests return true because there IS a row, it’s just an empty one, so the header appears on ALL posts with nothing below it.

    I’ve even tried testing for a value in the first sub-field, which is blank if no rows are used, when I use that, then the header is not shown but the section does still show if there are rows.

    This is what I’m using:

    $external_resources = get_field('external_resources');
    if( have_rows('external_resources') ) {
    
    $exturl = get_sub_field('external_url');
    if(!empty($exturl)) {
    ?>
     <h5>Additional Resources:</h5>
    <?php } ?>    
        <ul>
        <?php while( have_rows('external_resources') ): the_row();  ?>
            <li><a href="<?php the_sub_field('external_url'); ?>" target="_blank"><?php the_sub_field('external_anchor_text'); ?></a></li>
        <?php  endwhile; } ?>
        </ul>
    

    How can I do this so that IF the rows are used, then it displays the header and the rows, but if there are no rows USED then it displays nothing.

    OR alternatively, how can I show NO rows on the Post Edit screen but have the “Add Rows” button if someone wants to add some rows for external resources……I have it set to “minimum” of 0 but it still shows one (empty) row on every post.

  • Hi @trisham

    That’s weird. The repeater should show one row only if you set the minimum to 1. Could you please share the JSON export of the field group so I can test it out on my end?

    Thanks 🙂

  • Hi James,

    Yes, it does show 1 (empty) row on the post edit screen, even though I have the minimum set to ‘0’. I don’t know how to make it NOT show a row, but instead just show the “Add Rows” button.

    The problem is that *because* it shows one row, whether or not that row is used, when I look at that post in my database via phpMyAdmin, it shows a value of ‘1’ (1 row) – it should either show ‘0’ or not even be there at all (no meta key field) IF the post author does not fill in anything on that row.

    But because it shows ‘1’ I can’t use that in my query, because the post author *could* use that row so ‘1’ could mean that there are sub-fields, or not. If I use ‘1’ to determine if the section with it’s header should be shown, it will always be shown, even when it hasn’t been used, so I get just the header, no links below, which is not what I want.

    Ideally it should behave as any other custom field – available to use on the Post, but not added to the database as a ‘key’ for that post unless it’s actually used.

    But for now I would be happy if I could just figure out how to query for that metadata, and show the header/section ONLY if data has been filled into the row – my test for data in a sub-field isn’t working. If the row isn’t filled in, I still see the two sub-fields in the database attached to that post, but they are blank….so in theory I could check for that, and if NOT blank, show the header and section, but it they ARE blank, don’t show it. It just isn’t working as I have it.

  • Hi @trisham

    The problem is that the repeater shouldn’t behave like that. I think it’s better to solve this issue than to find a workaround. Could you please share the JSON or XML export of your field group so I can test it out on my end?

    Also, Could you please try to reproduce the issue on one of the WordPress’ stock themes (like Twenty Sixteen) with other plugins deactivated? If it disappears, then you can activate the theme and plugins one by one to see which one causes the issue.

    Thanks 🙂

  • Hi James,

    I deeply appreciate your help!

    Below is the JSON export, and here are three screenshots that I hope you’ll find helpful. Unfortunately I cannot change the Theme or plugins, it’s a live site and the current Theme and plugins provide a good deal of the site’s functionality – the whole thing would break if I switch.

    That said, I really don’t think this is related to the Theme or other plugins, I believe that it’s simply my own inexperience with PHP (I’m a very novice coder) – I just don’t know how to structure my code so that it will show the header (“Additional Resources”) and then links IF there is data entered in at least 1 row, and not show either if there is not – I think looking at the screenshots will help the most, along with my code snippet above.

    Here are the screenshots:
    http://www.bestallinclusive.com/images/0_ScreenShot1.png
    http://www.bestallinclusive.com/images/0_ScreenShot2.png
    http://www.bestallinclusive.com/images/0_ScreenShot3.png

    Here’s my JSON export:

    [
        {
            "key": "group_55f2102aaf128",
            "title": "Post: Additional External Resources",
            "fields": [
                {
                    "key": "field_55f21041f438e",
                    "label": "External Resources",
                    "name": "external_resources",
                    "type": "repeater",
                    "instructions": "<p>Use this field to add OFF-site links and the text anchor for the link. You may use the same for both fields.  This will create a list of links below the post.<\/p>\r\n<p>Examples:<br>\r\nURL: http:\/\/urltomycoolsite.com\/  -  Anchor Text: My Cool Site<br>\r\nOR<br>\r\nURL: http:\/\/urltomycoolsite.com\/  -  Anchor Text: http:\/\/urltomycoolsite.com\/<\/p>",
                    "required": 0,
                    "conditional_logic": 0,
                    "wrapper": {
                        "width": "",
                        "class": "external-links",
                        "id": ""
                    },
                    "collapsed": "",
                    "min": "",
                    "max": 10,
                    "layout": "table",
                    "button_label": "Add More Rows",
                    "sub_fields": [
                        {
                            "key": "field_55f210bdf438f",
                            "label": "URL",
                            "name": "external_url",
                            "type": "url",
                            "instructions": "Enter the URL, including the http:\/\/",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "http:\/\/"
                        },
                        {
                            "key": "field_55f21102f4390",
                            "label": "Anchor Text",
                            "name": "external_anchor_text",
                            "type": "text",
                            "instructions": "Enter the Text that will be linked to the URL. May be the same as URL.",
                            "required": 0,
                            "conditional_logic": 0,
                            "wrapper": {
                                "width": "",
                                "class": "",
                                "id": ""
                            },
                            "default_value": "",
                            "placeholder": "",
                            "prepend": "",
                            "append": "",
                            "maxlength": "",
                            "readonly": 0,
                            "disabled": 0
                        }
                    ]
                }
            ],
            "location": [
                [
                    {
                        "param": "post_type",
                        "operator": "==",
                        "value": "post"
                    }
                ]
            ],
            "menu_order": 0,
            "position": "normal",
            "style": "default",
            "label_placement": "top",
            "instruction_placement": "label",
            "hide_on_screen": "",
            "active": 1,
            "description": ""
        }
    ]
  • Hi @trisham

    This is weird. I’ve just tested the field group on my end and it didn’t show any row when I created a new post. Could you please tell me if the row is there or not when you create a new post? Also, could you please remove the row manually and see what happens after that? You can remove the row by hovering the right column of the row. I’ve attached a screenshot for your reference.

    If the issue persists, I suggest you to create a staging or development site to do the theme and plugins test. You’ll be shocked if you know that even a simple function from other plugins/theme can cause something bad to a plugin.

    If you have tried all of them and still not joy, could you please open a new ticket and provide temporary admin credentials to the staging/development site? You can open it here: https://support.advancedcustomfields.com/new-ticket. Don’t forget to include the link to this thread in the ticket.

    Thanks 🙂

  • Eureka!

    To answer your question, yes – 1 row did appear on all ‘new’ posts when created (initially). My theory now is that on the first post, I did use that field for 1 row.

    However, I removed the row from a Post in which it was not being used, so there is no row and just the two field headers and a “Add a Row” button. I updated my instructions for that field to tell authors to start by clicking the Button, which gives them a row to use. If they’re not using that field, they just don’t click the button.

    NOW when I click on “add new” (post) the row is NOT there, just the field names, my instructions, and the button, AND my code now WORKS! It only shows my <h5> header and the list of links IF that field is actually used.

    Also in checking in my database, the field (meta key) is still there (external_resources) with a value of 0 and no sub-fields (rows) which is fine.

    Thank you very much – such a simple fix, it would never have occurred to me to just remove the unused row…..jeesh! <blush>

    🙂

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

The topic ‘Can I have a repeater field with NO rows’ is closed to new replies.