Home › Toolset Professional Support › [Resolved] Using get_post_meta in cred_save_data
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 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | 9:00 – 12:00 | - |
- | 13:00 – 18:00 | 13:00 – 18:00 | 13:00 – 18:00 | 14:00 – 18:00 | 13:00 – 18:00 | - |
Supporter timezone: America/Jamaica (GMT-05:00)
This topic contains 21 replies, has 2 voices.
Last updated by nicolaS-3 2 years, 5 months ago.
Assisted by: Shane.
Hi all,
I am writing a function triggered by cred_save_data where I have the following line:
$field1 = get_post_meta($post_id, 'wpcf-user-type', true);
It works, but because the user type it's a select field it returns the field content (ID) instead of the display text, how can I get the display text instead ?
Also, how can I get the image from a multi-image custom field (any image of the array would do) and set that one as featured image ?
thanks
Regards
Nicola
Hi Nicola,
Thank you for getting in touch.
Can you try using our Types API function for this.
$field1 = types_render_field("user-type", array('item'=> $post_id ));
For this you will perhaps need to use the same type_render_field but this will return an array of results. From there you can pick off which one you want and use it as the featured image.
Example
$imageurl = types_render_field("image-field", array('item'=> $post_id, 'index' =>0, 'url' => true)); //gets first image $imageid = attachment_url_to_postid($imageurl); set_post_thumbnail($post_id,$imageid);
Above is a basic example of how to get this done. Please let me know if this helps.
Thanks,
Shane
Hi Shane
thanks for your indications that possibly would solve my issue, but when I test the snippet and get whatever PHP error my site crashes and I can't even access it via FTP. I must ask my provider to delete the snippet file to get the site back. I have activated TOOLSET_CODE_SNIPPETS_TEST_MODE but I don't understand how this helps. Can you explain me how to test a snippet avoiding to crash the whole site ? the only solution I can think of is to create a local copy of the site so that I can delete the snippet code by myself.
thanks
Regards
Nicola
Hi Shane
I found an old localhost toolset site that I can use for testing the snippet. Its objective is simply to write some field contents alltogether in the excerpt field. I don't get errors anymore, but the excerpt field is empty. Could you please tell me what's wrong in this code ? thanks
<?php /** * New custom code snippet (replace this with snippet description). */ toolset_snippet_security_check() or die( 'Direct access is not allowed' ); // Put the code of your snippet below this comment. add_action('cred_save_data','write_excerpt_profiles',10,2); function write_excerpt_profiles($post_id,$form_data) { if ($form_data['id']==20753) { $sometext = get_post_meta($post_id, 'post_content', true); $phone1 = types_render_field("negozio_telefono", array('item'=> $post_id )); $phone2 = get_post_meta($post_id, 'wpcf-negozio_telefono2', true); $excerpt= $sometext . ', ' . $phone1 . ', ' . $phone2 $args = array('ID' => $post_id, 'post_excerpt' => $excerpt); wp_update_post($args); } }
please wait, I might have found the solution by myself, thanks
Hi Nicola,
Thank you for the update.
Please let me know if you need any further input from my side.
Thanks,
Shane
Hi Shane
I managed to have the above function working with manageable errors on local copy of the site. Types_render_field works, but types_render_termmeta doesn't. I have a taxonomy called user-age with just one value selected, I cannot get the selected value with this code, could you please tell me what's wrong ? thanks, regards
$term_ids = wp_get_post_terms( $post->ID, 'user-age', array( 'fields' => 'ids' ) ); $age = types_render_termmeta("user-age", array( 'term_id' => $term_ids[0] ));
Hi Nicola,
Assuming that this is still inside the form. This code below should return a list of ID's correct?
$term_ids = wp_get_post_terms( $post->ID, 'user-age', array( 'fields' => 'ids' ) );
How are the ids stored in the custom field? Are they stored as a comma separated list? Can you send me a screenshot of what the IDs would look like in the field ?
Thanks,
Shane
Hi Shane,
Q. How are the ids stored in the custom field?
actually I don't know because the variable is always empty. Ages it's a normal flat taxonomy of age ranges and users always select one only. See screenshot. I was wondering if I should change
array( 'fields' => 'ids' )
into
array( 'fields' => 'name' )
or
array( 'fields' => 'slug' )
because I don't have any field called "IDs". What I am expecting to see in return is the name of the selected age range: eg: 26-35
thanks
Nicola
Hi Nicola,
actually I don't know because the variable is always empty.
There is the problem. This means that there is something wrong with the logic of how you're getting the term ids. The custom field 'user-age' is being used to store the value of the term id.
Would you mind allowing me to have admin access to the website so that I can have a more detailed look at this for you ?
Please where applicable please provide me with a link to an example page where I can see the issue.
I've enabled the private fields for your next response.
Thanks,
Shane
Hi Nicola,
I see the issue. You can't use types_render_termmeta to get the taxonomy that is attached to the post.
For this case the appropriate function would be this one below.
https://developer.wordpress.org/reference/functions/get_the_terms/
Using this will return the taxonomies that are attached to your post.
Thanks,
Shane
Hi Shane
I've tried:
$age = apply_filters( 'get_the_terms', $age, $post_id, 'ages' );
--- -> the variable $age is empty
The above should be the right one but in other examples I see many people use this:
$age = get_the_terms($post_id->ID, 'ages' );
-----> system crashes !
I also read this about get_post_terms:
https://toolset.com/forums/topic/displaying-taxonomy-term-on-front-end/
so I tried also
$age = wp_get_post_terms( $post->ID, 'ages', array( 'fields' => 'nome' ) );
--- -> the variable $age returns the word "Array" (I supposed "nome" to be the taxonomy field name, see previous attachment, just a guess ...)
Any clue ? thanks
Nicola
Hi Nicola,
An array is in fact what the get_the_terms() function will return as defined by the wordpress codex. What is in this is an array of terms.
If you perform a var_dump on the $age function i.e var_dump($age); you should be able to see the array of terms.
What exactly do you want from the array of terms is it the ID ? as you should be able to pick this off example.
$age[0]->term_id
Thanks,
Shane
Hi Shane
could you please be more specific ? I am not a PHP programmer I am trying just to copy some code to get this done, I've lost a week already trying these two lines of code ! Honestly I don't see why Toolset doesn't have a "types_render_taxonomy" equivalent of "types_render_field" ....
I want to get the term_name. I have tried
$age = wp_get_post_terms( $post->ID, 'ages', array( 'fields' => 'term_name' ) );
and
$age = wp_get_post_terms( $post->ID, 'ages', array( 'fields' => '$age[0]->term_id' ) );
both crash the system ....
Thanks
Regards
Nicola
Hi Nicola,
We don't have a types_render_taxonomy function because this will essentially be redundant as the wordpress default function works with all taxonomies even custom taxonomies. Also there isn't any need for special processing.
However to make things a lot simpler you can use the taxonomy shortcode since you're not a PHP programmer.
$age = do_shortcode( "[wpv-post-taxonomy item='".$post->ID."' type='ages' separator=',' format='name' ]");
Please try this and let me know if this works. The shortcode should provide you with the list of taxonomy names.
Thanks,
Shane