Skip Navigation

[Resolved] get_post_meta used to work…until I added a new post field

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 7 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 replies, has 2 voices.

Last updated by malcolmG 7 years, 11 months ago.

Assisted by: Beda.

Author
Posts
#381676

I am trying to: Figure out why code worked before and now it doesn't when a new field was added.

I visited this URL: hidden link

I expected to see: a Floorplan image for the second to last unit (668 sq.ft.)

Instead, I got: a blank?

Here was the code that worked fine until I added the new field

// Floorplans								
echo '<div class="threecol first"><div class="twocol first"><img src="'.home_url().'/wp-content/uploads/2014/05/view-floorplan.gif" /></div>';
$floorimgs = get_post_meta($child_post->ID, 'wpcf-multi-floor-plans', false);
	if(!empty($floorimgs))
	{
	echo '<div class="tencol first"><a href="'. $floorimgs[0] .'" rel="prettyPhoto['.$child_post->ID.'-gallery]">';
	echo '<img width="100%" src="' . $floorimgs[0] . '" class="floor-thumb" />';
	$floorimgs = array_slice($floorimgs,1);
		foreach ((array)$floorimgs as $floorimg) 
		{
			echo '<a href="'.$floorimg.'" rel="prettyPhoto['.$child_post->ID.'-gallery]" style="display:none">';
			echo '<img src="'.$floorimg.'" /></a>';
		}
	echo '</a></div></div>';
	} else {
	echo '<div class="tencol first"><a href="'.$child_post->fields['floorplan'].'" rel="prettyPhoto"><img src="'.$child_post->fields['floorplan'].'" class="floor-thumb" width="100%" /></a></div></div>';
	}

The odd thing is, on sections that don't use the new field (ie. the last unit type (514 sq.ft.)) the code still works as it used to. The old units suddenly seem to be treated as if they have the $floorimgs as you can see in the addition of -gallery] to the href.

Why would adding a new field that is completely unrelated to this field cause this issue?

Any suggestions?

#381739
get_post_meta()

works fine with any Custom Field, it does not matter if a new, old or even if Types / Not Types Field

The WordPress function will always fetch the Field that you tell it to fetch.

So it must be a issue with your Post ID, or the Field Slug, or what it returns.

I can not see how this is setup, and we do not provide Custom Code Solutions or debug in the Free Types Forum.

What is as example returning this below code?

$child_post->ID

I do not see in your code how you define this $child_post variable and that is crucial for you to analyze so to be sure the ID is the correct one.

You also pass 'false' to your Custom Field get_post_meta(), so it will not return a single value.

If you analyze your Source code you will see, no URL is passed to the img src, it is left out empty.
Also href is empty.

Means, your

$floorimg

is returning nothing.

This can not be due to just adding a Field with types that is not related to your content.

It's not possible because if the field is not related, and not queried in $floorimg, it can also not interfere.
Unless of course, the field has the exact same slug as the queried one and belongs to the same post ID.

But as mentioned, this is Custom Code and can not be debugged by us in this Free Forum, where we support the Free Types Plugin and it's features.

To design Content created with types you can either user the Provided ShortCodes, or, you can use Views and Content templates, which should allow you to do pretty much the same you do now with PHP; but all from within the WordPress Admin, with no PHP.
https://toolset.com/home/toolset-components/
https://toolset.com/documentation/user-guides/view-templates/
https://toolset.com/documentation/user-guides/views/

If you need assistance with Custom PHP Code you can consult the DO Cfor the Types API:
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://toolset.com/documentation/user-guides/displaying-wordpress-custom-fields/#1

Also, we can recommend the certified partners in case you need more assistance with Custom PHP:
https://toolset.com/consultant/

You will get the custom assistance you need to get on with your project.

Thank you for understanding.

#382125

Thanks Beda,

I can clarify

$child_posts = types_child_posts('unit');

My understand of the 'false' in get_post_meta is that it will instead of returning a single value, it will return an array (if it exists). This code did in fact perform it's function correctly until I added the new fields (and applied them within my code).

I agree and was surprised that there should be no conflict as the fields are totally unrelated...but yet, it still happened. It worked great before, I just can't figure out why it doesn't work now?

In the meantime, I've figured out a low tech workaround...it just isn't ideal though.

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.