Fetch custom field data for a specific blog post - wordpress

I want to output the metadata from a custom field in WordPress post.
On this page if WordPress codex I found the following instruction:
To fetch meta values use the get_post_meta() function:
get_post_meta($post_id, $key, $single);
I am trying to do it this way:
<?php
get_post_meta(1, 'Currently Reading',true);
?>
But nothing gets output in the browser.
What's the proper way to output the content of the custom field?

The easiest way to do this is this:
<?php echo get_post_meta($post->ID, 'your_meta_key', true); ?>
On your post or page editor, you can go to "Screen Options" in the top right corner and check the box to display "Custom Fields". This will allow you to see the meta keys available. Just copy the name of the meta key into your get_post_meta call in the spot above where it says "your_meta_key". Don't change the $post->ID as that is global.

Taken from that page linked
<?php $meta_values = get_post_meta( $post_id, $key, $single ); ?>
so you'd need to access it through the $meta_values return object.
Like so:
<?php
print_r($meta_values);
print 'The ID is: ' . $meta_values->ID;
?>

get_post_meta(1, 'Currently Reading',true); will only get the values, you need to store it somewhere and output it properly. One way to do this is to store the function return values into a variable like so:
<?php $custom = get_post_meta( 1, $key, $single ); ?>
Then you can output it with a print or echo like so:
echo $custom;
Something to note, try using a value $post_id for the first argument. This will grab the current post id.

Related

how to show the data of CPT UI plugin on website?

I have used CPT UI to add some posts with taxonomies. I have filled two post data in CPT UI for practice. Now I want to show these post on a page. What all code I have to write.
You can use Wp_Query along with the post name that you created using CPT Ui plugin to display those posts. Like, e.g. i had created a post named as school then code to display all posts of School type is as following :
$query = new WP_Query( array( 'post_type' => 'school' ) );
while($query->have_posts()):
$query->the_post();
echo $query->ID; // it will print the ID of post
endwhile;
Hope this will clear the things..
In order to pull in the custom fields/post meta, you will need to write some code within the WordPress loop (https://codex.wordpress.org/The_Loop) in your template file.
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.
eg.
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
For all post meta:
$meta = get_post_meta( get_the_ID() );
echo '<pre>';
print_r( $meta );
echo '</pre>';
Or for a single value:
$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_key_name', true );
See below for more information in the WordPress Codex:
https://codex.wordpress.org/Custom_Fields
and
https://developer.wordpress.org/reference/functions/get_post_meta/

Custom Fields/Meta Boxes - Only Display If Value is Present

I am using the Custom Metaboxes and Fields for WordPress add-on to create custom fields for custom post types. However, I need a way to display content only if a value exists for a specific custom field.
Currently, I am using this code:
<?php
$url = get_post_meta($post->ID, 'snippet-reference-URL', true);
if ($url) {
echo "<p><a href='$url'>Reference URL</a></p>";
} ?>
However, this displays content if the field is present (which in this case, is always). I need code that will only display content if a specific field has a value.
check this example given on codex page
<?php
$key_1_value = get_post_meta( get_the_ID(), 'key_1', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo $key_1_value;
}
?>

get value of image custom field at frontent of wordpress?

Please help me out.I am battling since three days.
How to fetch value of a custom field to display it in post content area?
You can get custom field to display using below code:
get_post_meta($post_id, $key, $single);
Custom field data can be fetched inside template (inside loop) using this functions:
Fetches all fields
<?php the_meta(); ?>
OR for specific value
get_post_meta($post_id, $key, $single);
You can learn more + examples in official wordpress documentation about custom fields.
You can use this through below code:
<?php echo get_post_meta($post_id, $key, true); ?>
Here your arguments must same as you have mention in function.php, while creating your custom field like as in:
<?php add_post_meta($post_id, $key, $single,true); ?>
Thanks.

Advanced custom field (wordpress plugin of same name) not showing

I've set up some fields using the advanced custom fields.
I’ve created a custom field and a post that uses that custom field. I’m trying to display it on a page like this:
<?php
$args = array( 'post_type' => 'Portfolio Item' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<p>' . the_title() . '</p>';
echo '' . the_field('portfolio_url') . '';
endwhile;
?>
The title displays no problem, but the custom field does not i.e. the output is just:
The name ‘portfolio_url’ is the ‘Field Name’.
Can anyone help with what I’m doing wrong?
Maybe you should try and send in smaller snippets of code.
Or give an online example.
Basically if you add a the_field('bottom_quote') function on your page it should echo out the current pages' "bottom_quote" field.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
<?php the_field( 'bottom_quote', $post->ID );
Also note that $post should either be global or in a foreach loop.
I don't think the post_type parameter is allowed to have a space. Check that you're using the correct slug for that first.
I am not to familiar with this specific plugin but you may need to call in the global $variable that I know when using a class like WPAlchemy you need to call $meta
Check here http://codex.wordpress.org/Function_Reference/get_post_meta

Wordpress Shortcode in custom field within custom post type

Hi Guys Need help with this very badly.
Need to add shortcode to output in the area circled in white in the picture below.
And the input area is under video description. And from my understanding ive have confirmed that the name for that text area is description_value.
I have looked through every documentation and tried all filters and do_shortcode variations to no avail. Please help i have spent 3 days non stop doing this. Puting the codes in my function.php and so many others. It still does not parse [shortcodes] it just displays text "[shortcodes]". please refer to picture below
Thank you.
This is outputing on the page. I have
<div class="describe-feat">[postexpirator]</div>
This is in a file called grid-gallery.js
<h2><%= item.title %></h2></div><div class="view-gallery">\
<div class="describe-feat"><%=item.desc%></div>\
<% if(item.imgnum){ %><span class="item-num"><%= item.imgnum %></span><% } %>\
This is in custom post editor in wordpress admin area
<textarea name="description_value" class="option-textarea">[postexpirator]</textarea>
https://www.dropbox.com/s/almn09e1dwmeywb/shortcodxe.jpg
It's because, you are missing do_shortcode function for parsing shortcode.
Assuming you just want to target a single value, you could just do this inside the loop.
<?php echo ( do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , true ) ) ); ?>
If your post has multiple values for that custom field, then you can set the above to false.. and loop over the array...
<?php $values = do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , false ) ); ?>
<?php if($values && is_array($values)) : ?>
<?php foreach( $values as $meta) : ?>
<p><?php echo $meta ?></p>
<?php endforeach; ?>
<?php endif; ?>
Just want to add that if you won't use the_post() no shortcode will work,
I had this issue when trying to enable shortcode on CutomFields on a new page type, and nothing worked until activating WP loop with the_post() .

Resources