Closed sapel closed 10 years ago
I just came here to report this same exact issue.
Possibly related to #2046
I just tested and can cofirm this issue. I have two users in forums reporting it as well.
If I select one item in the multi-select field, I get back an array with 2 indexes both with the same value. If I select more than one, I get an array with the correct values.
Here's a workaround:
$pods = pods( 'renter_housing', $post->ID );
$housing = $pods->field('housing_category');
if ( is_array( $housing ) && isset( $housing[1] ) && $housing[0] === $housing[1] ) {
echo "<ul><li>" . $housing[0] . "</li></ul>";
} else {
echo '<ul><li>' . implode('</li><li>', $housing) . '</li></ul>';
}
Forum threads with reports of this: http://pods.io/forums/topic/broken-2/ http://pods.io/forums/topic/custom-defined-list-database-error/
Database screen grabs to illustrate issue.
This is when I select only one value (z) in multi-select
Here is when I select two options ( x and z) in the multi-select:
This helps, I'll take a look at this part of the code.
Hi,
I am getting this same problem. I was going to create a new issue, but luckily I found this.
It only works when I have more than one box selected. Whenever I have just one box selected, that selected value would just be repeated multiple times. The amount of times it is repeated depends on how many checkboxes were selected during that last time the post was saved.
Debug Information
WordPress Version: 3.9.2 PHP Version: 5.2.17 MySQL Version: 5.1.73 Server Software: Apache Your User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.5; rv:16.0) Gecko/20100101 Firefox/16.0 Session Save Path: /tmp Session Save Path Exists: Yes Session Save Path Writeable: Yes Session Max Lifetime: 1440 WPDB Prefix: wp_ WP Multisite Mode: No WP Memory Limit: 40M Pods Network-Wide Activated: No Pods Install Location: /home/metro/public_html/wp-content/plugins/pods/ Pods Tableless Mode Activated: No Pods Light Mode Activated: No Currently Active Theme: Metro Theme Currently Active Plugins: Advanced Code Editor: 2.2.5 Captcha: 4.0.3 Contact Form 7: 3.9.1 Content Aware Sidebars: 2.4.1 GARD Pro: 2.4.2 Geo Mashup: 1.8.0 heatmap for WordPress: 0.2.2 Pods - Custom Content Types and Fields: 2.4.3 Preserved HTML Editor Markup: 1.5 Rank Tracker: 1.9 Redirection: 2.3.6 SEO Booster 2: 2.0.10 Wordfence Security: 5.1.8 WordPress Front-end Editor: 0.10 Wordpress Popular Posts: 3.0.3 WordPress SEO: 1.5.5 WP-Optimize: 1.8.6 WP Lightbox 2: 2.28.8.7
@asap247 Thanks for the additional info. Multiple users have been able to successfully implement the workaround I've listed above. For now, you should use that.
Based on the workaround by @Shelob9, my code works, but I would prefer to display the Labels instead of the values form the Custom Defined list. This is my full code:
/* Services Offered */
add_action( 'genesis_entry_content', 'services');
function services() {
global $wp_query;
global $post_id;
$post_id = $wp_query->post->ID;
$services = get_post_meta($post_id,'services', false);
if ( is_array( $services ) && isset( $services[1] ) && $services[0] === $services[1] ) {
echo "Services Offered:<ul><li>" . $services[0] . "</li></ul>";
} else {
echo 'Services Offered:<ul><li>' . implode('</li><li>', $services) . '</li></ul>';
}
}
I would prefer it would get the Labels instead.
value|Label
My Custom defined list: payment_center|Payment Center phone_vendor|Phone Vendor phone_financing|Phone Financing
@asap247 If you used Pods::display()
instead of get_post_meta to get the field, it would give you the label.
Something like this would work, if you change 'NAME_OF_POD' to the correct name:
/* Services Offered */
add_action( 'genesis_entry_content', 'services');
function services() {
global $wp_query;
global $post_id;
$post_id = $wp_query->post->ID;
$pods = pods( 'NAME_OF_POD', $post_id );
$services = $pods->display( 'services' );
if ( is_array( $services ) && isset( $services[1] ) && $services[0] === $services[1] ) {
echo "Services Offered:<ul><li>" . $services[0] . "</li></ul>";
} else {
echo 'Services Offered:<ul><li>' . implode('</li><li>', $services) . '</li></ul>';
}
}
Also, I'm not sure whey you're using the global $wp_query object to get the global post object? Why not go directly to it with global $post;
Thanks @Shelob9, I updated my code with the global $post.
/* Services Offered */
add_action( 'genesis_entry_content', 'services');
function services() {
global $post;
$services = get_post_meta(get_the_id(),'services', false);
if ( is_array( $services ) && isset( $services[1] ) && $services[0] === $services[1] ) {
echo "Services Offered:<ul><li>" . $services[0] . "</li></ul>";
} else {
echo 'Services Offered:<ul><li>' . implode('</li><li>', $services) . '</li></ul>';
}
}
However, when I use Pods::display(), I get an error.
Warning: implode() [function.implode]: Invalid arguments passed in /home/metro/public_html/wp-content/themes/metro/single-locations.php on line 231
This is the code I use when the error appears:
/* Services Offered */
add_action( 'genesis_entry_content', 'services');
function services() {
global $post;
$pods = pods( 'locations', get_the_id());
$services = $pods->display( 'services' );
if ( is_array( $services ) && isset( $services[1] ) && $services[0] === $services[1] ) {
echo "Services Offered:<ul><li>" . $services[0] . "</li></ul>";
} else {
echo 'Services Offered:<ul><li>' . implode('</li><li>', $services) . '</li></ul>';
}
}
Line 231 is:
echo 'Services Offered:<ul><li>' . implode('</li><li>', $services) . '</li></ul>';
Pods::display always returns a string.
@sc0ttkclark If I use the following code;
/* Services Offered */
add_action( 'genesis_entry_content', 'services2');
function services2() {
global $post;
$pods = pods( 'locations', get_the_id());
$services = $pods->display( 'services' );
if ( is_array( $services ) && isset( $services[1] ) && $services[0] === $services[1] ) {
echo $services[0];
} else {
echo $services;
}
}
the Pods:: display returns a string; however, I want to display each item using an ordered list instead of a string with commas & the word 'and'. The code by @Shelob9 works but it only pulls up the value and not the label.
Is there any way I can use pods::display to display the labels in an ordered or bullet'ed list (ul/li)?
@asap247 There isn't a direct way to do this. @sc0ttkclark seems like we need a way for field()
or display()
to output an array of labels instead of values, from a simple custom relationship. That's a separate issue though.
@asap247 Since your values are so similar to your labels, you can use field() and just manipulate the output via PHP to get it to have spaces and caps. Something like this:
add_action( 'genesis_entry_content', 'services2');
function services2() {
global $post;
$pods = pods( 'locations', get_the_id());
$services = $pods->field( 'services' );
if ( is_array ( $services ) ) {
foreach ( $services as $key => $service ) {
$service = str_replace( '_', ' ', $service );
$service = ucwords( $service );
$services[ $key ] = $service;
}
}
if ( is_array( $services ) && isset( $services[1] ) && $services[0] === $services[1] ) {
$services = $services[0];
}
echo 'Services Offered:<ul><li>' . implode('</li><li>', $services) . '</li></ul>';
}
Thanks @Shelob9, works great. I had to slightly tweak the code to get it to work for when one field is selected, but thanks I appreciate your help. It would be great to have pods::display or pods::fields to have a parameter option to choose between displaying values vs labels in the future.
The code I used to get the code working properly.
/* Services Offered */
add_action( 'genesis_entry_content', 'services');
function services() {
global $post;
$pods = pods( 'locations', get_the_id());
$services = $pods->field( 'services' );
if ( is_array ( $services ) ) {
foreach ( $services as $key => $service ) {
$service = str_replace( '_', ' ', $service );
$service = ucwords( $service );
$services[ $key ] = $service;
}
}
if ( is_array( $services ) && isset( $services[1] ) && $services[0] === $services[1] ) {
echo "Services Offered:<ul><li>" . $services[0] . "</li></ul>";
} else {
echo 'Services Offered:<ul><li>' . implode('</li><li>', $services) . '</li></ul>';
}
}
Fixed via 70c77d5 & 9227bc6
wordpress - 3.9.1 pods - 2.4
To repro:
Expected: an array with the one selected item. Actual: an array with the current value duped the same number as was the previous # of selections.
This is what's going on in the database:
mysql> select * from wp_postmeta where post_id=9 \G
*** 16. row *** meta_id: 657 post_id: 9 meta_key: _pods_meal_type meta_value: a:2:{i:9;s:6:"dinner";i:10;s:6:"salads";} *** 17. row *** meta_id: 665 post_id: 9 meta_key: meal_type meta_value: dinner *** 18. row *** meta_id: 666 post_id: 9 meta_key: meal_type meta_value: dinner