Custom listing attributes added through PHP code are not saving

Following the code snippet provided in this thread (Is there any plugin for choosing a country? - #4 by yevhen) I have added three custom select box attributes, adding the options via code. The select boxes display properly and are populated with the options, but the values are not being saved. Originally I thought they maybe needed matching key => value data, but I’ve tested that and it doesn’t change anything.

Steps to reproduce

Create custom select attribute and add options via code (vs adding manually). Edit the listing (front end) and save.

Actual result

The data is not saved and therefore not displayed.

Expected result

The data to be saved and display in the view listing.

Extra details

It appears to be saving if edited via wp-admin, but not when editing on the front end. And saved data is not displayed on the front end (in front end editing or view).

Hi,

Please try to add the '_external' => true parameter to this attribute in the edit_field and check if it works.

Hi andrii,

I’ve added that parameter, and now the field is not displaying at all. I’ve var_dumped right after adding the field and it looks correct here? Am I missing something else?

        var_dump($attributes['country']);

Thanks.

Hi,

Please provide more details. Is this new attribute already added in Listings > Attributes or are you adding it via code as well? Also, what exactly are you trying to customize, the list of countries?

Hello,

The attribute is added through the wp admin (Listings > Attributes) originally as a select box with no options added. The options are then added in the code (see code in previous screenshot).

Do I need to create the attribute via code for this to work?

I have three separate select boxes (added through wp admin) with custom options (added through the code) that I am trying to get saving.

I hope that clarifies the details a bit more, but let me know what else you need to know!

Thanks

Yes, it will not work this way because attributes registered via the admin UI expect the taxonomy terms instead of hard-coded options. Please try adding a new attribute via the code, you can do this via the same hivepress/v1/models/listing/attributes hook. You can use these parameters for the edit_field:

'label'     => 'Country',
'type'      => 'select',
'options'   => 'countries',
'_external' => true,

This will add countries to the drop-down automatically, or you can define options as an array of custom items.

Hope this helps.

Excellent, I’ve got it working now. Thanks a bunch!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.