get_post_meta with save_post or publish_post action hook - wordpress

I am trying to get_post_meta from wp_postmeta to copy to a plugin table wp_wpgmza when a post is published.
Using a save_post action hook correctly copies dat from the wp_post table but I get blank fields for wp_postmeta.
I have tried using a publish_post action hook but this also returns blanks.
This is the latest function I have been;
function map_update($ID, $post) {
//if ($post->post_type = 'post') return;
$link = get_permalink ( $ID );
$title = get_the_title ( $ID );
$mapaddress = get_post_meta ( $ID, 'address', true );
global $wpdb;
$table = $wpdb->prefix . 'wpgmza';
$data = array(
'link' => $link,
'title' => $title,
'address' => $mapaddress,
);
$wpdb->insert($table , $data);
}
add_action('publish_post', 'map_update' );

please check I cahnged argument to $post_id may be you get your solution
function map_update($post_id) {
$ID=$post_id;
//if ($post->post_type = 'post') return;
$link = get_permalink ( $ID );
$title = get_the_title ( $ID );
$mapaddress = get_post_meta ( $ID, 'address', true );
global $wpdb;
$table = $wpdb->prefix . 'wpgmza';
$data = array(
'link' => $link,
'title' => $title,
'address' => $mapaddress,
);
$wpdb->insert($table , $data);
}
add_action('publish_post', 'map_update' );

Related

Display Custom Order Filter In WooCommerce

I am trying to display a custom admin order column in WooCommerce and I am having a hard time getting the code right here.
I have this code:
add_filter( 'woocommerce_admin_order_data_after_order_details', 'display_order_data_in_admin', 10, 1);
function display_order_data_in_admin( $output )
global $wp_query
$output .= wc_product_dropdown_categories( array(
'show_option_none' => 'Filter by Location',
'taxonomy => 'product_tag',
'name' => 'product_tag',
'selectd' => isset( $wp_query->query_vars['product_tag'] ) ? $wp_query->vars['procut_tag'] :
) );
return $output;
}
I am having a hard time displaying a column for filtering my orders by location that has been selected in checkout.
add_filter( 'manage_edit-shop_order_columns', 'wc_add_new_order_admin_list_column' );
function wc_add_new_order_admin_list_column( $columns ) {
$columns['billing_country'] = 'Country';
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'wc_add_new_order_admin_list_column_content' );
function wc_add_new_order_admin_list_column_content( $column ) {
global $post;
if ( 'billing_country' === $column ) {
$order = wc_get_order( $post->ID );
echo $order->get_billing_country();
}
}

Updating A Post Title Using GET Method and $wpdb Class

I have a programming exercise and I wanted to check if what I have done so far is the best way to update a post title based on setting the query string. In my example below I create a link for updating the post and validate the origin with a nonce. I used the template_redirect action as it only fires once where as the wp and parse_query actions seemed to fire multiple times. The GET request can come from a form submit or link click but I've only attached the link example but the code should work for both situations.
function update_post_title() {
global $wpdb;
$post_id = get_query_var( 'post_id', 0 );
$post_title = get_query_var( 'post_title', '' );
$post_nonce = get_query_var( 'update_post_title_nonce', '' );
if( ! empty( $post_title ) AND $post_id > 0 ) {
if ( ! wp_verify_nonce( $post_nonce, 'update_post_title_action' ) ) {
wp_die( 'Security check failed' );
}
$post_title = sanitize_text_field( $post_title );
//$post_slug = sanitize_title_with_dashes( $post_title );
$post_id = intval( $post_id );
$rows_affected = $wpdb->update(
$wpdb->posts,
array( 'post_title' => $post_title ),
array( 'ID' => $post_id ),
array( '%s'),
array( '%d' )
);
}
}
function add_custom_query_vars( $public_query_vars ) {
$public_query_vars[] = 'post_id';
$public_query_vars[] = 'post_title';
$public_query_vars[] = 'update_post_title_nonce';
return $public_query_vars;
}
add_filter( 'query_vars', 'add_custom_query_vars' );
add_action( 'template_redirect', 'update_post_title' );
Sample link generate.
global $post;
<a href="<?php echo wp_nonce_url( add_query_arg( array(
'post_id' => 123,
'post_title' => 'The New Title', ),
site_url( $post->post_name ) ),
'update_post_title_action',
'update_post_title_nonce' );
?>">Click To Update</a>

Set Author of a saved post as its parent post author

I try to change author of saved harmonogram item with author of parent but those to functions seems not to work - empty i think. When i set the author id with constant value everything works.
$parent_post_id = wp_get_post_parent_id($post_id);
$post_author_id = get_post_field( 'post_author', $post_id );
Here is whole function
function func_auto_update_post_author( $post_id ) {
$post_type = get_post_type($post_id);
if ( "harmonogram" != $post_type ) return;
//$parent_post_id = get_queried_object_id();
$parent_post_id = wp_get_post_parent_id($post_id);
$post_author_id = get_post_field( 'post_author', $parent_post_id );
$my_post = array(
'ID' => $post_id,
'post_author' => $post_author_id,
);
remove_action('save_post', 'func_auto_update_post_author');
wp_update_post($my_post);
add_action( 'save_post', 'func_auto_update_post_author');
}
add_action( 'save_post', 'func_auto_update_post_author');
Try below Code :
function func_auto_update_post_author( $post_id )
{
$post_type = get_post_type($post_id);
if ( "harmonogram" != $post_type ) return;
$parent_post_id = wp_get_post_parent_id($post_id);
if($parent_post_id)
{
$post_author_id = get_post_field( 'post_author', $parent_post_id );
$my_post = array(
'ID' => $post_id,
'post_author' => $post_author_id
);
wp_update_post($my_post);
}
}
add_action( 'save_post', 'func_auto_update_post_author');
you can track if any error occurred during post update by putting below code in above action hook:
$post_id = wp_update_post( $my_post, true );
if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo $error;
}
}
Hope this will help!

Wordpress filter post before displaying

Currently all posts is being listed by category,
i need to filter the list by a meta-key. I used the filter('posts_where') but all the query has been changed.
I need to add the WHERE condition in the existing sql that is being generated.
function get_filtered_post( $args, $meta, $value ){
$posts = get_posts( $args );
$ids = array();
foreach( $posts as $post ){
$id = $post->ID;
if( get_post_meta( $id, $meta, true ) == $value ){
$ids[] = $id;
}
}
return $ids;
}
$args = array( 'post_type' => 'post', 'posts_per_page' => -1 );
$IDofPost = get_filtered_post( $args, 'my-meta-key', 'the-metas-value' );
foreach( $IDofPost as $id ){
echo get_the_title( $id );
}
Hope this function will help. It returns post id with the given post meta values.

Add first link from post in custom field - Wordpress

I m trying to add the first link from the post content to a custom field. Something is wrong and I ve been trying to get it to work but no luck. What should I do to add the link to "link" custom field?
add_action( 'publish_post', 'check_post' );
function check_post( $post_id ) {
$user_info = get_userdata(1);
function get_first_link() {
global $post, $posts;
preg_match_all('/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $links);
return $links[1][0];
}
$first_link = get_first_link();
add_post_meta($post_id, 'link', $first_link, true);
add_post_meta($post_id, 'users', $user_info->user_login, true);
}
EDIT
I got it working halfway. It saves the url, but it doesn't save it when the post is published. It saves it on update. What do I need to use? I tried using publish_post but that saved the url on update also.
add_action( 'save_post', 'my_save_post', 10, 2 );
function my_save_post( $post_id, $post ) {
if ( wp_is_post_revision( $post_id ) )
return;
$matches = array();
preg_match_all( '/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $matches );
$first_link = false;
if ( ! empty( $matches[1][0] ) )
$first_link = $matches[1][0];
$user_info = get_userdata(1);
$meta_link = $_POST['link'];
$args = array(
'post__not_in'=> array($id),
'post_type' => 'post',
'post_status' => array('publish'),
'meta_query' => array(
array(
'key' => 'link',
'value' => $first_link,
'compare' => '='
)
)
);
$existingMeta = get_posts( $args );
if(empty($existingMeta)){
//Go ahead and save meta data
update_post_meta( $post_id, 'link', esc_url_raw( $first_link ) );
update_post_meta($post_id, 'users', $user_info->user_login);
}else{
//Revert post back to draft status
update_post_meta( $post_id, 'link', $existingMeta[0]->ID );
update_post_meta($existingMeta[0]->ID, 'users', $user_info->user_login);
//Now perform checks to validate your data.
//Note custom fields (different from data in custom metaboxes!)
//will already have been saved.
$prevent_publish= true;//Set to true if data was invalid.
if ($prevent_publish) {
// unhook this function to prevent indefinite loop
remove_action('save_post', 'my_save_post');
// update the post to change post status
wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
}
}
}
Here you go:
add_action( 'save_post', 'my_save_post', 10, 2 );
function my_save_post( $post_id, $post ) {
if ( wp_is_post_revision( $post_id ) )
return;
$matches = array();
preg_match_all( '/href\s*=\s*[\"\']([^\"\']+)/', $post->post_content, $matches );
$first_link = false;
if ( ! empty( $matches[1][0] ) )
$first_link = $matches[1][0];
update_post_meta( $post_id, 'link', esc_url_raw( $first_link ) );
}

Resources