WordPress echo post meta array value inside input - php

I'm sending array meta box data using update_post_meta like below. However I cannot seem to output the post meta array value into an empty input. The meta is being stored correctly.
if( get_post_meta( $post->ID, 'date-meta', true ) ) {
$date_info = get_post_meta( $post->ID, 'date-meta', true );
}
My input field looks like this:
<input type="date" class="widefat" name="vp-date" id="vp-date" value="<?php echo $date_info['vp-date']; ?>" />
I also get a notice which traces back to the if get_post_meta function above. It says:
Trying to get property of non-object in
Any help would be great.
Thanks

That error message means that the $post is $post->ID is not an object. I don't know what script you are in but try putting
global $post;
above the if.
Making it
global $post;
if( get_post_meta( $post->ID, 'date-meta', true ) ) {
$date_info = get_post_meta( $post->ID, 'date-meta', true );
}

Related

Echo a custom page's custom field outside the loop

I have a custom page template with certain custom fields. I want to display these custom fields outside the loop, but within the same page.
This one works:
<?php echo get_post_meta( '244', 'custom_field_name', true ) ?>
But I want to to work dynamically, without me entering the actual ID of the page.
How can I call the page ID in the echo?
Try this :
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field', true);
wp_reset_query();
?>
If this call is in the loop, replace the id with function get_the_ID, this function retrieves the ID of the current item in the WordPress Loop.
<?php echo get_post_meta( get_the_ID(), 'custom_field_name', true ) ?>
See: https://developer.wordpress.org/reference/functions/get_the_ID/
If this call is in the single page, replace the id with object item $post->ID.
$post = get_post();
<?php echo get_post_meta( $post->ID, 'custom_field_name', true ) ?>
See: https://codex.wordpress.org/Class_Reference/WP_Post
Also, you can get the access to the post via global variable $post.
$post = get_post();
<?php echo get_post_meta( $post->ID, 'custom_field_name', true ) ?>
See: https://codex.wordpress.org/Global_Variables

Unable saving an associative array using add_post_meta() function

I need to run an encryption function on each member of an associative array prior to it being saved to wp_postmeta table. My form allows dynamic add/delete of rows. I've been trying to get this to work using array_walk().
This is how the form is configured:
<input type="text" id="z_my_data[][username]" name="z_my_data[0][username]">
<input type="text" id="z_my_data[][password]" name="z_my_data[0][password]">
This is ran on the 'save_post` action:
// Save encrypted data to post meta
if (isset($_POST['z_my_data'])) {
// Get posted form variables
$my_data = $_POST['z_my_data'];
// Encrypt each member of each row
for ($i = 0; $i < count($my_data); $i++) {
$cryptKey = $this->cryptKey;
array_walk($my_data[strval($i)], create_function('&$val', 'global $cryptKey; $val = Crypto::encrypt($val, $cryptKey);'));
}
if ( ! add_post_meta( $post_id, '_my_data', $my_data, true ) ) {
update_post_meta( $post_id, '_my_data', $my_data);
}
} else {
delete_post_meta( $post_id, '_my_data' );
}
I can see that the data is being encrypted. Here is a print_r($my_data) after the for loop:
Array
(
[0] => Array
(
[username] => ®ØåÛâÏ0…"ë°?mˤÙ
[password] => xSFç„L¶·3z˜'J0ÖRÅÎj
)
)
But the post meta key is not created and no error is generated. The meta key doesn't exist in the postmeta table, yet add_post_meta() returns false and the key/value is never added.
Does anyone see what I'm doing wrong?
I'm not sure how to tell, but I think the [0] is a named key and not an index key. I say that because I can create more than one and delete the [0] element and the single element remaining still shows [1] using print_r.
In your if ( ! add_post_meta( $post_id, '_my_data', $my_data, true ) ) { you are using add_post_meta() function and last argument is set to true.
It should be false instead, because you are inserting an array and NOT a string.
For this reason your if statement is not working properly.
Instead, your code should be (as add_post_meta last argument default value is false):
if ( ! add_post_meta( $post_id, '_my_data', $my_data ) ) {
update_post_meta( $post_id, '_my_data', $my_data );
}
Alternatively, you could also use this:
if ( ( !empty( get_post_meta( $post_id, '_my_data' ) ) ) {
update_post_meta( $post_id, '_my_data', $my_data );
}
References:
WordPress Code Reference - add_post_meta
WordPress Code Reference - update_post_meta
WordPress Code Reference - get_post_meta

Updating custom field not working properly

I have a custom code to update a custom field from the front end. It loads the data correctly but when I try to change/update the custom field it goes wrong. It updates the meta value, but it has more values.
These are the meta values:
sp_metrics'a:3:{s:15:"ledennummerknvb";s:5:"12659";s:6:"height";s:1:"5";s:6:"weight";s:1:"5";}'
When I try to update "ledennummerknvb" it goes terribly wrong.
Here's the code I use to create the custom field:
function your_function_name( $form_id, $post_id, $form_settings ) {
$value = '';
if ( $post_id ) {
$metrics = get_post_meta( $post_id, 'sp_metrics', true );
$ledennummerknvb = $metrics['ledennummerknvb'];
}
?>
<div class="wpuf-label">
<label>Ledennummer KNVB</label>
</div>
<div class="wpuf-fields">
<input type="text" name="my_custom_field" value="<?php echo( $ledennummerknvb ); ?>">
</div>
<?php
}
add_action( 'my_brand_new_hook', 'your_function_name', 10, 3 );
function update_my_brand_new_hook( $post_id ) {
if ( isset( $_POST['my_custom_field'] ) ) {
update_post_meta( $post_id, 'sp_metrics', $_POST['my_custom_field'] );
}
}
add_action( 'wpuf_add_post_after_insert', 'update_my_brand_new_hook' );
add_action( 'wpuf_edit_post_after_update', 'update_my_brand_new_hook' );
So when I update the custom field it overwrites every value plus it will give me back a result of just 1 number. For instance now you see at "ledennummerknvb" the numbers are "12659" but when I change or update them it goes wrong.
I hope someone can help and or explain me what I'm doing wrong as I don't have the knowledge to figure it out.

get_post_meta - WordPress

This code is designed to add a button to specific posts using the get_post_meta function. How do I alter the get_post_meta function to display this button on a specific post? I have already tried changing its $post->ID parameter to '1464', which is the post ID I want to use.
function custom_listify_single_job_listing_actions_after() {
global $post;
$url = get_post_meta( $post->ID, 'your_custom_meta_key', true );
echo 'My Button';
}
add_filter( 'listify_single_job_listing_actions_after', 'custom_listify_single_job_listing_actions_after' );
If you only want to run this code on a specific post, you need to add an if statement to check for that post ID.
Your code would need to look similar to this:
if($post->ID == 1464){
$url = get_post_meta( $post->ID, 'your_custom_meta_key', true );
echo 'My Button';
}
This simply wraps the get_post_meta() function and echo statement so that both of these only run on the post you want them to. Any other post will ignore the code.

Add Html code with get post meta

I am confused with a simple issue, Actually, I am using Wordpress Custom fields using post meta
echo get_post_meta( $post->ID, '_text_field', true );
Above meta code generate a value entered in a custom field.
But, I want to add html code around it.
If i add paragraph to it like this <p> <?php echo get_post_meta( $post->ID, '_text_field', true ); ?> </p> then it is fine! but when field is empty then it shows a empty <p></p>
That's why i am looking to add a html in meta code so that if field is empty then no html code will be included in DOM.
And if field have text then it shows text with a html markup.
i am not sure how to do this!
Any idea?
Use the wpautop() function to automatically add paragraphs instead.
Example:
echo wpautop( get_post_meta( $post->ID, '_text_field', true ) );
You should probably be assigning this meta field to a variable first and checking the value is set before you attempt to output.
E.g.
$text_field = get_post_meta( $post->ID, '_text_field', true );
if ( ! empty( $text_field ) ) {
echo wpautop( $text_field );
}

Categories