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.
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.
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
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.
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
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) {
...
No log after I added error_log('title: ' . $title);
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.
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:
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.
Ok Thanks!! Let me know once you guys get the solution.
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;
}
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.
Yes It is working without the workaround.