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
Related
I've set up two different custom post types – one for categories and one for posts. I've attached the posts to the categories with the CMB2 plugin. I now want to display all the attached/related posts on each category page. I'm able to display the different ID's from the array, but not the post content.
Trying to get attached posts:
$attached_users = get_post_meta( get_the_ID(), 'pr2_cmb2_attached_posts', true );
foreach ( $attached_users as $user ) {
$employee = get_post( $user );
}
Trying to display the content from the attached posts
<?php while ( have_posts() ) : the_post(); ?>
<?php echo get_the_title($employee);?>
<?php echo get_the_post_thumbnail($employee);?>
<?php endwhile; // end of the loop. ?>
The plugin you are using gives you an array of ids.
Using this ids gives you the possibility to get the post object by using get_post($id).
The function gives you a post object: https://developer.wordpress.org/reference/functions/get_post/
But if you just want to use functions like get_the_title() you only need the post id, not the whole post object.
Well, you already got all the data you need and do not need to get the post object.
The plugins gives you an array of post ids.
// get array of post ids
$attached_users = get_post_meta( get_the_ID(), 'pr2_cmb2_attached_posts', true );
// loop through posts using post ids
foreach ( $attached_users as $user ) {
echo get_the_title( $user );
echo get_the_post_thumbnail( $user, 'thumbnail' );
}
I'm using Woocommerce Bookings with only a base cost filled. No other rules. How do I display product price in a loop like this?
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
ID is: <?php the_ID(); ?>
Title is: <?php the_title(); ?>
Price: <?php echo esc_html( get_post_meta( get_the_ID(), '_regular_price', true ) ); ?>
<?php endwhile; wp_reset_query(); ?>
The price field didn't show anything
UPDATE
another code I tried:
<?php
global $woocommerce;
$product = new WC_Product_Booking($productID);
$base_price = $product->get_price();
$product_data = $product->get_data();
$product_pricing = get_post_meta( $product_id, '_wc_booking_pricing', true);
?>
<?php echo $product_pricing; ?>
<?php echo $base_price;?>
both also return the value of zero
ANOTHER TRY
<?php
global $woocommerce;
$product = new WC_Product_Booking($productID);
$product = wc_get_product( $product_id );
$base_price = $product->get_price();
$product_data = $product->get_data();
$product_pricing = $product_data['pricing'];
foreach($product_pricing as $key => $princing ){
$pricing_type = $princing['type'];
$pricing_base_cost = $princing['base_cost'];
$pricing_base_modifier = $princing['base_modifier'];
$pricing_cost = $princing['cost'];
$pricing_modifier = $princing['modifier'];
$pricing_from = $princing['from'];
$pricing_to = $princing['to'];
}
$pricing_data = get_post_meta( $product_id, '_wc_booking_pricing', false); ?>
<?php echo $pricing_data; ?>
nothing works :( they're all either showing blanks or showing zero.
Can anyone help point in the right direction?
many thanks
Try (from the docs):
$product = new WC_Product_Booking($productID);
//don't know if you need 'echo'
echo $product->get_price_html()
If not, I think the logic of 2nd snippet it's ok, but probably productID != postID,
so another solution would be:
find the postID for that productID
call get_post_meta( postID, '_wc_booking_pricing', true);
print the price
EDIT
My answer's too long for being posted in the comments.
Are you sure that you are set correctly the product from wp-admin?
In order to do the second suggestion, you need access to the database.
Get the ID of the post(product) in wp-admin when you create/modify it, you should have in the url something like {URL}/wp-admin/id=123.
Once you have the ID go in wp_post_meta table in the DB and look for all the fields with that post_id, and check if you can find the correct price.
Once you find it, you can call get_post_meta(correctID,correctKey)
It seems like you're pulling the right meta key, could be related with you using esc_url(), when you should be using just esc_html(). I'd try that.
I'm trying to display a custom field on an email.
Here's the script that I thought should work:
<?php if ($day) : ?>
<p><strong><?php _e('day:', 'woocommerce-pip'); ?></strong> <?php echo $order->day; ?></p>
<?php endif; ?>
But it doesn't work. The name of the field is 'day'.
Should this work? What am I missing?
Thanks.
I assume that you have already set correctly this custom field on checkout page with some code that displays the custom field and save the value of this custom field within the order in database (if not, you will get nothing).
You need first to retrieve the order ID before trying to get the value of this custom field and there can be 2 cases:
You have already the $order variable or object. You simply retrieve it with:
$order_id = $order->id;
// or
$order_id = str_replace( '#', '', $order->get_order_number() );
You don't have the $order object, you will need to get it first:
global $woocommerce, $post;
$order = new WC_Order( $post->id );
// and after you can get the order ID
$order_id = $order->id;
// or
$order_id = str_replace( '#', '', $order->get_order_number() );
Now you can get the value and display it because you have $order_id:
<?php $day = get_post_meta( $order_id, 'day', true );
if ( !empty( $day ) ) : ?>
<p><strong><?php _e('day:', 'woocommerce-pip'); ?></strong> <?php echo $day; ?></p>
<?php endif; ?>
But you should check in your database within the order_id as post_id in wp_postmeta table, that a meta_value => 'day' exist with some value in the corresponding meta_value row/column. If not, there is certainly a problem with your code when you create this custom field and save the value on checkout page.
For display custom field like this
<?php echo get_post_meta( get_the_ID(), ‘tes_custom_field’, true ); ?>
for more information check this vary good tutorial http://www.themelocation.com/how-to-display-custom-field-value-on-product-page-in-woocommerce/
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.
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 );
}