Skip Navigation

[Resolved] wpt_field_options stopped working

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

Problem: The custom options I have applied to a CRED field using the wpt_field_options filter are no longer working.

Solution: Update to the latest version to fix the bug with the wpt_field_options filter.

This support ticket is created 7 years, 1 month 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 16 replies, has 3 voices.

Last updated by Christian Cox 6 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#586725
Screenshot_1.png

I have code to display custom post type in user filed dropdown.Below code was working till now..Suddenly it stopped working.

add_filter('wpt_field_options', 'prefix_custom_options', 10, 3);

function prefix_custom_options($options, $title, $type) {
    switch ($title) {
        case 'Working at member':
            $options = array();
            $args = array(
                'post_type' => 'member',
                'post_status' => 'publish',
                'posts_per_page' => -1);
            $posts_array = get_posts($args);
            /*  Change start */
            $options[] = array(
                '#value' => 0,
                '#title' => "Select",
            );
            /*  Change end */
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            break;
        case 'Committee':
            $options = array();
            $args = array(
                'post_type' => 'committee',
                'post_status' => 'publish',
                'posts_per_page' => -1);
            $posts_array = get_posts($args);
            /*  Change start */
            $options[] = array(
                '#value' => 0,
                '#title' => "Select",
            );
            /*  Change end */
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            break;
    }
    return $options;
}

Reference ticket : https://toolset.com/forums/topic/post-relationship-with-cpt-and-user/

Please do reply ASAP as it is urgent.

#586910

Hmm, I don't see anything wrong with the code. In fact, I copied it and pasted it into my local Toolset Starter environment using the same Toolset plugin versions, then created some member and committee posts and added the two custom fields to a 3rd custom post type. They seem to be working as expected, I can see member and committee posts showing up as options in the select fields. So something else must be going on. Please try these steps:
- Confirm you have published, public Member and Committee posts
- Confirm the post type slugs are still 'member' and 'committee'
- Temporarily deactivate all plugins except Types and retest. If the problem is resolved, reactivate plugins one by one until the conflict is revealed. If the problem was not resolved, continue below.
- Open the browser inspector and watch for any errors that appear on this page in wp-admin. Copy + paste those in your reply for me to review.
- Activate server-side logging and paste any relevant log data in your reply here. If you are not familiar with logging, go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Then reload this page in wp-admin. If any server-side errors are generated, this will create an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the updates you made to wp-config.php.

#586951

Hi,

I have tried all above options. error_log.txt file is also blank.

- Confirm you have published, public Member and Committee posts
Yes
- Confirm the post type slugs are still 'member' and 'committee'
Yes
- Temporarily deactivate all plugins except Types and retest. If the problem is resolved, reactivate plugins one by one until the conflict is revealed. If the problem was not resolved, continue below.
Done
- Open the browser inspector and watch for any errors that appear on this page in wp-admin. Copy + paste those in your reply for me to review.
Done - no errors
- Activate server-side logging and paste any relevant log data in your reply here. If you are not familiar with logging, go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it
Done
- Then reload this page in wp-admin. If any server-side errors are generated, this will create an error_log.txt file in your site's root directory.
Done - no errors

Still issue is there

Thanks

#587220

Okay thanks. This means we need to set up some more logging to determine where the breakdown is occurring. Please add logs as shown below:

function prefix_custom_options($options, $title, $type) {
  error_log('prefix_custom_options called');  
  switch ($title) {
        case 'Working at member':
          error_log('case working at member');  
          $options = array();
            $args = array(
                'post_type' => 'member',
                'post_status' => 'publish',
                'posts_per_page' => -1);
            $posts_array = get_posts($args);
            /*  Change start */
            $options[] = array(
                '#value' => 0,
                '#title' => "Select",
            );
           error_log('posts');
            error_log(print_r($posts_array, true));
            /*  Change end */
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            error_log('options');
            error_log(print_r($options, true));
            break;
        case 'Committee':
            error_log('case committee');
            $options = array();
            $args = array(
                'post_type' => 'committee',
                'post_status' => 'publish',
                'posts_per_page' => -1);
            $posts_array = get_posts($args);
            /*  Change start */
            $options[] = array(
                '#value' => 0,
                '#title' => "Select",
            );
            error_log('posts');
           error_log(print_r($posts_array, true));
            /*  Change end */
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            error_log('options');
            error_log(print_r($options, true));
            break;
    }
    return $options;
}

Activate server logs, reload the admin page, and observe the results. Let me know what logs, if any, get generated related to this function.

#587385

Generated error log is as mentioned below.

[08-Nov-2017 04:31:26 UTC] prefix_custom_options called
[08-Nov-2017 04:31:26 UTC] prefix_custom_options called
[08-Nov-2017 04:31:26 UTC] prefix_custom_options called
[08-Nov-2017 04:31:26 UTC] prefix_custom_options called
[08-Nov-2017 04:31:26 UTC] prefix_custom_options called
[08-Nov-2017 04:31:26 UTC] prefix_custom_options called

#587607

Okay so the filter is being triggered, but it seems the title parameter doesn't match the strings provided in the switch statement. Can you add one more log message to reveal the title parameter being passed into the function?

function prefix_custom_options($options, $title, $type) {
  error_log('prefix_custom_options called');  
  error_log('title: ' . $title);
  switch ($title) {
  ...
#587628

No log after I added error_log('title: ' . $title);

#587645

That doesn't make sense...can I create a clone of your site and see what's going on? If that's okay, please provide login criteria in the private reply fields here.

#588354

I see below error log.
[10-Nov-2017 04:02:54 UTC] prefix_custom_options called
[10-Nov-2017 04:02:54 UTC] title:
[10-Nov-2017 04:02:54 UTC] prefix_custom_options called
[10-Nov-2017 04:02:54 UTC] title:
[10-Nov-2017 04:02:54 UTC] prefix_custom_options called
[10-Nov-2017 04:02:54 UTC] title:
[10-Nov-2017 04:02:54 UTC] prefix_custom_options called
[10-Nov-2017 04:02:54 UTC] title:
[10-Nov-2017 04:02:54 UTC] prefix_custom_options called
[10-Nov-2017 04:02:54 UTC] title:
[10-Nov-2017 04:02:54 UTC] prefix_custom_options called
[10-Nov-2017 04:02:54 UTC] title:

#588750

Any update?

#588928

Hi, I didn't receive your login credentials, but another ticket from last week reported a similar issue where this filter is not working as expected any longer. I suspect you are running into the same bug. Our 2nd tier support team has escalated that issue to our developers, and I'm waiting to receive some additional information. I'll update you as soon as I have something to share. Sorry for the inconvenience here.

#589011

Ok Thanks!! Let me know once you guys get the solution.

#589139

I have fixed it using below link.
https://wordpress.org/support/topic/bug-report-wpt_field_options-title-is-empty-when-select-field-for-term-field/
Correct code

add_filter('wpt_field_options', 'prefix_custom_options', 10, 3);

function prefix_custom_options($options, $title, $type) {

  if (empty($title) || $title=='') {
		$wptoolset_field = $GLOBALS['wptoolset_field'];
		$title = str_ireplace('wpcf-','', $wptoolset_field->getId());
	}
  switch ($title) {
        case 'working-at-member': 
          $options = array();
            $args = array(
                'post_type' => 'member',
                'post_status' => 'publish',
                'posts_per_page' => -1);
            $posts_array = get_posts($args);
            $options[] = array(
                '#value' => 0,
                '#title' => "Select",
            );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            break;
        case 'committee':
            $options = array();
            $args = array(
                'post_type' => 'committee',
                'post_status' => 'publish',
                'posts_per_page' => -1);
            $posts_array = get_posts($args);
            $options[] = array(
                '#value' => 0,
                '#title' => "Select",
            );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            break;
    }
    return $options;
}
#589499

Okay great. Today's plugin updates include a permanent fix for this problem, so the workaround should no longer be necessary. Please update to the latest versions and let us know if the problem persists without the workaround you implemented.

#589789

Yes It is working without the workaround.