Skip Navigation

[Resolved] Custom field conditionally stopped displaying

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user has a select field with dynamic option values. The options of the field are generated using Toolset Types filter wpt_field_options. The generated options are the months of the last two years.
Inside a view, the field is not displayed anymore.

Solution:
It turns out that the values that are not displayed are not included in the values generated by the wpt_field_options filter. Toolset returns an empty value.

The solution is to use a custom shortcode that will return the row value from the database.

// The shortcode needs to be added in Toolset->Settings->Custom Code.
add_shortcode('meta', 'meta_fun');
function meta_fun($atts){
  global $post;
  $atts = shortcode_atts(array(
    'field' => NULL,
  ), $atts);
  extract($atts);
  if( NULL === $field ) return;
  // return "Voila";
   
  return get_post_meta($post->ID, $field, true);
}

// How to use it inside the view
[meta field="wpcf-production-year-month"]

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

This support ticket is created 3 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 3 replies, has 2 voices.

Last updated by Kostas 3 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#1979477
Screen Shot 2021-03-07 at 17.53.14.png

Hi,

I have a search view which displays cpt "Oliveoils", based on several quality criteria. Since the beginning of this year, custom field "production-year-month" stopped displaying in the view, for 2018 or older Oliveoils (please see attached image), and in the Template for these Oliveoils.

The Select type of "production-year-month" is defined in simEVOO plugin, lines 220-250. As Oliveoils are created in the frontend, a user cannot create 2018 or older Oliveoils, which somehow affects their display in the search view and in the template.

To recreate attached image, please check "Blend" first entry (Όχι -> No) and click on the "Αναζήτηση" (->Search) button. Last entry on the table should have been 2018-12, instead of the empty space. It exists in the database, but is not displayed for some reason.

Any ideas of what might have gone wrong?

Thank you in advance,
Kostas

#1979557

Hello Kostas and thank you for contacting the Toolset support.

To better assist you with this, I need to check how it is implemented. Would you allow me temporary access to this staging site? Your next reply will be private to let you share credentials safely. Please share the basic auth user/password too.

This is a view, right? Please share the URL of it in your next message.

#1979773

Thank you for the credentials.

Because you are filtering the values in the simEVOO plugin, lines 220-250, the current value of that post(2018-12) is not part of the options for the select. That's why Toolset shortcode does not return it.

I created a custom shortcode in the Toolset->Settings->Custom Code area that returns the raw data from the database. hidden link

add_shortcode('meta', 'meta_fun');
function meta_fun($atts){
  global $post;
  $atts = shortcode_atts(array(
    'field' => NULL,
  ), $atts);
  extract($atts);
  if( NULL === $field ) return;
  // return "Voila";
  
  return get_post_meta($post->ID, $field, true);
}

Then I used it in the view's template instead of the Toolset shortcodes. It gives the expected results hidden link

[meta field="wpcf-production-year-month"]

Note that you have to prefix the field's slug with "wpcf-".

I hope this helps. Let me know if you have any questions.

#1980127

That did the trick!
Excellent job, thanks a lot Jamal.

Cheers,
Kostas