custom field value is blank - wordpress

function add_custom_fields($post_id) {
global $post;
$metadescription = wp_trim_words( get_the_content(), 55 );
add_post_meta( $post_id, 'meta_description', $metadescription, true );
}
add_action( 'wp_insert_post', 'add_custom_fields' );
what i going wrong?

Below is a code that will add_post_meta every time a post or page is created or updated on your website.
function add_custom_fields($post_id) {
// If this is a revision, don't add content.
if ( wp_is_post_revision( $post_id ) )
return;
global $post;
$metadescription = wp_trim_words( apply_filters('the_content', $post->post_content), 55, '');
add_post_meta($post_id, 'meta_description',$metadescription, true);
}
add_action( 'wp_insert_post', 'add_custom_fields', 10, 1 );

Related

Notice: Trying to get property of non-object On WordPress save_post acion hook

I am creating a plugin where I need to update the input field post meta when the actual post is updated. So here is my code:
function save_meta_function ( ) {
global $post;
$post_id = $post->ID;
$meta_values = get_post_meta( $post_id );
foreach ($meta_values as $key => $value) {
update_post_meta( $post_id, $key, $_POST[$key] );
}
}
add_action( 'save_post', 'save_meta_function' );
but it's showing several errors:
Notice: Trying to get property of non-object // that's $post_id
= $post->ID;
Can you tell me why $post_id = $post->ID; line showing that error?
Replace your code with follows -
function save_meta_function ( $post_id ) {
$meta_values = get_post_meta( $post_id );
foreach ($meta_values as $key => $value) {
update_post_meta( $post_id, $key, $_POST[$key] );
}
}
add_action( 'save_post', 'save_meta_function' );

Custom Meta Field Has Stopped Working

I have a custom meta checkbox in a custom post type that until this afternoon was working fine, I went into add some new post types and the checkbox won't save I did add the isset value in the show_meta but it was still working after I did this and does not start working again if I remove it, and nothing is showing up in my debug can anybody help?:
function add_tiles_meta_box() {
add_meta_box(
'tiles_meta_box', // $id
'Show in Large Tiles?', // $title
'show_tiles_meta_box', // $callback
'tiles', // $screen
'normal', // $context
'high' // $priority
);
}
add_action( 'add_meta_boxes', 'add_tiles_meta_box' );
function show_tiles_meta_box( $post, $metabox ) {
$tiles = get_post_meta( $post->ID, 'tiles', true );
wp_nonce_field( basename(__FILE__), 'tiles_meta_box_nonce' );
?>
<!-- All fields will go here -->
<p>
<input type="checkbox" name="tiles" <?php echo (isset($_POST['tiles'])?"value='on'":"value='off'")?> <?php checked( $tiles, 'on' ); ?>/> Yes this is a large tile.
</p>
<?php
}
$post_ID = $post->ID;
function save_tiles_meta($post_ID) {
$post_ID = (int) $post_ID;
$post_type = get_post_type($post_ID);
$post_status = get_post_status( $post_ID );
if ($post_type) {
update_post_meta($post_ID, "tiles", $_POST["tiles"]);
}
return $post_ID;
}
add_action( 'save_post', 'save_tiles_meta' );
Your save_tiles_meta function is unnecessarily convoluted. Something may be failing along the way. Here's a more standard way to do that:
add_action( 'save_post', 'save_tiles_meta' );
function save_tiles_meta(){
global $post;
update_post_meta( $post->ID, 'tiles', $_POST['tiles'] ); // On or Off
}
Try with below code
add_action( 'save_post', 'save_tiles_meta',99,1 );
function save_tiles_meta($postId){
global $post;
update_post_meta( $post->ID, 'tiles', $_POST['tiles'] ); // On or Off
}

Show Custom Fields under "Add to Cart" button

I have using this function code to make new Custom Text Area, but have issue to show it in product page.
// Custom Field Product
add_action( 'woocommerce_product_options_general_product_data',
'woo_add_custom_general_fields' );
add_action( 'woocommerce_process_product_meta',
'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'Custom Text:', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' )
)
);
echo '</div>';
}
// Save Changes to DB
function woo_add_custom_general_fields_save( $post_id ){
// Textarea
$woocommerce_textarea = $_POST['_textarea'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_textarea', esc_html( $woocommerce_textarea
) );
}
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
// custom content.
echo get_post_meta( $post->ID, '_textarea', true );
}
Looks like when press save, its storing data in DB, but its not showing in single product page. Can someone to tell me where is issue in this function?
The problem is that in the following function, $post is not defined. (The code was indented for clarity.)
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
// custom content.
echo get_post_meta( $post->ID, '_textarea', true );
}
So, a simple fix would be to add global $post; to the function:
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
global $post;
// custom content.
if ( $post ) {
echo get_post_meta( $post->ID, '_textarea', true );
}
}
Alternatively, you can use the global $product object:
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
global $product;
// custom content.
if ( $product ) {
echo get_post_meta( $product->get_id(), '_textarea', true );
}
}

Woocommerce hook after product is published

I'm using below
add_action('transition_post_status', 'my_product_update', 1000, 3);
function my_product_update($new_status, $old_status, $post) {
if($new_status == 'publish' && $post->post_type == "product") {
$product_id = $post->ID;
$product = new WC_Product($product_id);
echo $product->get_price();
}
}
I figure it out that $product->get_price() is string(0) "".
I think it beacause I retrieve the price before it's saved.
But I can retrieve the name by using $product->post->post_title
Any idea how I can get the price right after the product is published?
Thank you.
Try This :
function wpa104760_default_price( $post_id, $post ){
echo $price = get_post_meta( $post_id, '_regular_price', true);
echo $sale = get_post_meta( $post_id, '_sale_price', true);
// exit;
}
add_action( 'woocommerce_process_product_meta', 'wpa104760_default_price',1000,2 );

External link from custom fields on a specific page

I want to have one page with the list with titles of posts. And every title should link to external page (using custom field named 'link'). I have this code, but it changes the link everywhere on the site and I want it to work only on a specific page, let's say on a page named "Example". On a homepage and everywhere except the page "Example" it should link to the post.
add_filter( 'post_link', 'links', 10, 2 );
function links( $link, $post )
{
$meta = get_post_meta( $post->ID, 'link', TRUE );
$url = esc_url( filter_var( $meta, FILTER_VALIDATE_URL ) );
return $url ? $url : $link;
}
You can check for queried_object on global $wp_query;
add_filter( 'post_link', 'links', 10, 2 );
function links( $link, $post )
{
$meta = get_post_meta( $post->ID, 'link', TRUE );
$url = esc_url( filter_var( $meta, FILTER_VALIDATE_URL ) );
if($wp_query->queried_object instanceof WP_Post && $wp_query->queried_object->ID == 258){
return $url ? $url : $link;
}else{
return $link;
}
}

Resources