update_post_meta for Wordpress Yoast SEO - php

I'm having trouble with Yoast Worpdress SEO to automatically Update the Title, Keywords and description,
I have tried several ways with no success,
First test I've done is something like this, adding directly in fucntions.php
update_post_meta('80', '_yoast_wpseo_title', 'Test SEO Title' );
It works fine, however when I try a method like this, it won't simply work
function save_seo_meta_data($post_id) {
global $post;
$data = new MovieData;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if ('movie_post' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
#wpseo_set_value('title', $data->seotitle, $post_id);
#WPSEO_Meta::set_value('title', $data->seotitle,$post_id);
update_post_meta($post_id, '_yoast_wpseo_title', $data->title );
}
add_action( 'save_post', 'save_seo_meta_data',9999999);
all of these three works using the above code when I change the meta key to different value
wpseo_set_value('title', $data->seotitle, $post_id);
WPSEO_Meta::set_value('title', $data->seotitle,$post_id);
update_post_meta($post_id, '_yoast_wpseo_title', $data->title );
I check the wp_postmeta value and can see all the value if I set different keys, but not when I point to yoast seo meta keys,
I've been also looking into its class and functions from here,
:https://github.com/Yoast/wordpress-seo/blob/ba4b1ad63f64d9658a2cc8de22b4391459423516/inc/class-wpseo-meta.php
:https://github.com/Yoast/wordpress-seo/blob/ba4b1ad63f64d9658a2cc8de22b4391459423516/inc/wpseo-functions.php
But still no success
Any help would be appreciated,

Try the wpseo_saved_postdata hook:
add_action( 'save_post', 'wpse26642385_save_post', 1, 1 );
function wpse26642385_save_post( $post_id )
{
// check stuff
// $data = ...
add_action( 'wpseo_saved_postdata', function() use ( $post_id ) {
if( function_exists( 'wpseo_set_value' ) ) {
update_post_meta( $post_id, '_yoast_wpseo_title', $data->title );
}
}, 999 );
}

Related

How do I add cookie value in order metadata in WooCommerce?

I want the value of cookie my_cookie in order metadata.
add_action('init','thankyou_grab_cookie_as_meta_data', 10, 1 );
function thankyou_grab_cookie_as_meta_data( $order_id ){
if( ! $order_id ){
return;
}
if(isset($_COOKIE["my_cookie"]) && ! get_post_meta( $order_id, 'some default value', true ) ){
update_post_meta( $order_id, 'some default value', esc_attr($_COOKIE["my_cookie"]) );
}
}
The metadata shown now in postman
I guess the problem is with the check on ! get_post_meta and then you update_post_meta while it doesn't exists yet. Have update the code below.
add_action('init', 'thankyou_grab_cookie_as_meta_data', 10, 1);
function thankyou_grab_cookie_as_meta_data($order_id) {
if (!$order_id) {
return;
}
if (isset($_COOKIE["my_cookie"])) {
$my_cookie = esc_attr($_COOKIE["my_cookie"]);
if (!get_post_meta($order_id, 'some_meta_key', true)) {
add_post_meta($order_id, 'some_meta_key', $my_cookie);
} else {
update_post_meta($order_id, 'some_meta_key', $my_cookie);
}
}
}
Seems like you are using the WP REST API, maybe you haven't registered the custom field yet. You can use register_rest_field
add_action('rest_api_init', 'custom_register_rest_field');
function custom_register_rest_field() {
register_rest_field(
'post', // post type
'some_meta_key', // meta key
);
}

WordPress : how add new post meta before saving product woocommerce

i have a api from another wocommerce website and take some information with my product sku
how can i add this information to post meta after created new product ?
i wanna when i create a product , the information take with api and product sku and save to post meta.
i found this hook but i think this not working
<?PHP
function my_product_att( $sku ) {
// my api codes and take information and use
add_post_meta('product_id','key','value');
// i haven't problem here i just need Appropriate hook
}
add_action( 'save_post', 'my_product_att' ); // this hook not work for woocommerce
Woocommerce Products are wordPress posts. You can use wordpress hooks like save_post & $post_id as its argument. You are passing $sku which is wrong.
add_action( 'save_post', 'wpse_110037_new_posts' );
function wpse_110037_new_posts($post_id){
$post_type = get_post_type($post_id);
if($post_type == 'products') {
add_post_meta($post_id,'key','value');
}
}
Try below code. Note that in code $unique is true or false based on if meta value should be unique or not.
add_action('transition_post_status', 'product_created_function', 10, 3);
function product_created_function($newstatus, $oldstatus, $post) {
if($oldstatus != 'publish' && $newstatus == 'publish' && !empty($post->ID) && in_array( $post->post_type, array( 'product') ) ) {
add_post_meta( $post->ID, $key, $value,$unique );
}
}
add_action( 'save_post', 'prowp_save_meta_boxdddd' );
function prowp_save_meta_boxdddd( $post_id ) {
global $wpdb;
$table_prefix = $wpdb->prefix;
$tablename = $table_prefix.'postmeta';
$wpdb->query($wpdb->prepare('UPDATE '.$tablename.' SET meta_value="test" WHERE post_id='.$post_id));
}

saved_post post_updated firing before custom fields are updated, what function fires after custom fields are updated? wordpress

I have a problem, I want to update a custom user meta data field using another custom post field. It has to happen AFTER the whole post INCLUDING CUSTOM FIELDS saves / updates. But all the calls I find: update the basic post data, then call the action, THEN update the custom fields.
saved_post & post_updated cause this code to be delayed by 1 saved. ie, if I were to make a new post and set the $my_value to 5, first time I saved it would come back with 0, then the next time it would come back with 5. ect ect.
Does anyone know of an action hook that runs after custom post data has been saved? or how to make save_post or post_updated to fire after the custom post data?
function zhu_li_do_the_thing( $post_id ) {
//get post data, extract author-ID and post-Type
$post = get_post( $post_id );
$post_type = $post->post_type;
$userid = $post->post_author;
//if your my custom post type, then get the custom field value i want.
if( 'my_custom_post_type' == $post_type ){
$post_custom_fields = get_post_custom($post_id);
$my_custom_field = $custom_fields['im_a_field'];
foreach($im_a_field as $key ){
$my_value = $key;
}
// if the value starts with a "+" or "-" do math with it against authors-custom-metadata, if it's null ignore it, if it's anything else, make the metadata = the value.
if(substr($my_value, 0,1)=='+' || substr($my_value, 0,1)=='-'){
$my_int = intval($my_value);
$author_int = intval(get_user_meta($userid, 'the_objective, true));
$result = ($author_int + $str);
update_user_meta( $userid, 'the_objective', $result );
}elseif($my_value == null ){
return;
}else{
update_user_meta( $userid, 'the_objective', $my_value )
}}};
add_action( 'save_post, 'zhu_li_do_the_thing' );
You should use pre_post_update to check the post meta before and use post_updated to check the updated post meta.
The following is a snippet of code from a class that checks the stock status of a woocomerce product.
function __construct()
{
add_action( 'post_updated', array( $this, 'post_updated' ), 1, 1 );
add_action( 'pre_post_update', array( $this, 'pre_post_update' ), 1, 1 );
}
function pre_post_update( $post_ID )
{
global $post_type;
if( $post_type == 'product' )
define( 'stock_status_previous', get_post_meta( $post_ID, '_stock_status', 1 ) );
}
function post_updated( $post_ID )
{
if( defined( 'stock_status_previous' ) )
{
$stock_status = get_post_meta( $post_ID, '_stock_status', 1 );
if( $stock_status == stock_status_check )
return;
// post meta is different - sp do something
}

How to save custom meta boxes in Wordpress

I've been trying to get my custom meta box data to save in Wordpress, and I haven't had any luck. I've tried researching other posts, but since everyone does it a little bit differently, I haven't had any success at using the tutorials and other posts out there.
I created a metabox:
add_action( 'add_meta_boxes', 'ic_add_heading_box' );
function ic_add_heading_box( $post ) {
add_meta_box(
'Meta Box',
'Heading Titles',
'ic_heading_box_content',
'page',
'normal',
'high'
);
}
function ic_heading_box_content( $post ) {
echo '<label>Main Heading (h1)</label>';
echo '<input type="text" name="heading_box_h1" value="" />';
echo '<label>Sub Heading (h3)</label>';
echo '<input type="text" name="heading_box_h3" value="" />';
}
I just can't for the life of me get the data I insert in to the fields to save in Wordpress. Any help would be greatly appreciated.
The function you are using is only a display function.
You are not actually doing nothing with the data . it is only for creating the metabox. not handling it.
You need to add
add_action( 'save_post', 'myplugin_save_postdata' );
and then use update_post_meta() with a function like in the codex example :
function myplugin_save_postdata( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions. If want
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
/* OK, its safe for us to save the data now. */
// Sanitize user input. if you want
$mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $mydata ); // choose field name
}

How can I get some of a posts meta data whilst it is still inserting?

I'm trying to make a function where a child post of a certain post type inherits the same title and slug as its parent. I'm using WP Types to define my post types and their relationships. But I'm having trouble with the below code:
function copy_parent_post_title( $post_id ) {
$new_post = get_post($post_id);
if($new_post->post_type == 'carnews-adverts') {
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_carnews_id', true );
$parent_title = get_the_title($parent_id);
$post_slug = sanitize_title_with_dashes($parent_title);
$post_update = array(
'ID' => $post_id,
'post_title' => $parent_title,
'post_name' => $post_slug
);
remove_action( 'wp_insert_post', 'copy_parent_post_title' );
wp_update_post( $post_update );
add_action( 'wp_insert_post', 'copy_parent_post_title' );
}
}
add_action( 'wp_insert_post', 'copy_parent_post_title' );
The problem is this line:
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_carnews_id', true );
I presume it is because at this point the post's meta data hasn't been inserted into the database yet? If so how can I achieve what I want by accessing the get_post_meta upon inserting a post?
Thanks
I think to access the '_wpcf_belongs_carnews_id' that is being added by WP Types, you need to look in the $_POST array. However, WP Types might call add_post_meta() after calling wp_insert_post(). In this case the meta data won't be present if you hook into wp_insert_post.
Instead, hook your function into add_post_meta:
function copy_parent_post_title( $post_id, $meta_key, $meta_value ) {
$new_post = get_post($post_id);
if (($new_post->post_type == 'carnews-adverts') &&
($meta_key == '_wpcf_belongs_carnews_id')) {
$parent_id = $meta_value;
$parent_title = get_the_title($parent_id);
// ... Rest of your function
}
}
add_action( 'add_post_meta', 'copy_parent_post_title', 10, 3 );
This may be completely wrong as I barely ever use Wordpress.

Categories