Support

Account

Home Forums Backend Issues (wp-admin) Disabling Select2 for Post Object fields

Solved

Disabling Select2 for Post Object fields

  • I need certain Post Object fields to be rendered as standard HTML dropdowns in the backend, not as Select2.

    I tried adding this action to acf/init:
    acf_update_setting(‘enqueue_select2’, false);
    This works great for regular Select fields – that is, regular Select fields that the have “Stylised UI” (i.e. Select2) option checked are rendered as standard HTML selection dropdowns. However, Post Object fields do not populate at all. They look like standard dropdowns, but the post values are not listed.

    Does anyone have an idea how to accomplish this?

  • I don’t think that you can use a post object field without using select2.

    My suggestion would be to use a standard select field where you dynamically generate the choices. https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

  • Thank you, John. I guess my options are to roll back to ACF4 or create a dynamically populated Select field. Unfortunately, I have the problem of 2,000+ existing posts with data in Post Object fields…

    I don’t mind rolling back to the functionality of ACF4, but I’m concerned for the long run, whether v4 will receive future security and WP compatibility updates, or if it’s a discontinued version.

  • I think that ACF 4 is going to be discontinued and will no longer be getting any updates.

    I would convert the current field to a dynamically populated select field.

    A post object field stores:
    If multiple selections are not allowed: A single post ID of the related posts
    If multiple are allowed: an array of Post IDs

    So your select field should have Post IDs as values and Post Titles as labels, this will mimic post object field, or you could set the label to something else.

    The main difference with the two fields is that you will not have ACF to automatically populate an array of post objects and you’ll need to do this yourself. It is a simple process of doing a WP_Query using the returned value or array in the ‘post__in’ argument. If it’s a single ID then you should post__in should be an array with a single value.

    If you convert the existing field(s) and you store the same values then all of your existing data should remain intact because the new field will have the same field key as the old field.

  • Other notes

    I would do this on a copy of the site, not on a live site, then move changes.

    You should check the DB values of these fields, I’m not sure if ACF stores and array of string values or an array of integers for the ID values for multiple selections. For single selections this does not matter. But you need to match the storage in the DB exactly.

  • Thank you, @john Huebner! I just tried this and it works beautifully. The good news is that multiple values were not enabled. The only wrinkle was that the field was set to return the Post Object instead of Post ID, so I had to adjust some code and replace get_field() calls with get_post(get_field()).

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

You must be logged in to reply to this topic.