there are a hundred posts to update the postmeta if I do manually. I try to find a way to insert new postmeta based on the post_type and category. I try this:
$args = [
'post_type' => 'post',
'category_name' => 'category'
];
$query = new WP_Query($args);
if( $query->have_posts() ) {
while ( $query->have_posts() ) {
add_post_meta($query->post->ID, '_meta_key', meta_value, true);
add_post_meta($query->post->ID, '_meta_key2', meta_value2, true);
add_post_meta($query->post->ID, '_meta_key3', meta_value3, true);
add_post_meta($query->post->ID, '_meta_key4', meta_value4, true);
add_post_meta($query->post->ID, '_meta_key5', meta_value5, true);
}
}
and it is not working. is there anyway to achieve ?
OK I found the answer here Trigger action when new post is insert in wordpress
Add meta value when post or page or custom post published.
function save_post_meta( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
if ( "your post name" != $post_type ){ return;}
update_post_meta( $post_id, 'meta_key', 'value');
}
add_action( 'save_post', 'save_post_meta', 10, 3 );
Use update_post_meta function to update post meta values.
$args = [
'post_type' => 'post',
'category_name' => 'category'
];
$query = new WP_Query($args);
if( $query->have_posts() ) {
$query->the_post();
while ( $query->have_posts() ) {
update_post_meta($query->post->ID, '_meta_key', meta_value, true);
update_post_meta($query->post->ID, '_meta_key2', meta_value2, true);
update_post_meta($query->post->ID, '_meta_key3', meta_value3, true);
update_post_meta($query->post->ID, '_meta_key4', meta_value4, true);
update_post_meta($query->post->ID, '_meta_key5', meta_value5, true);
}
}
Related
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!
I made some changes in a plugin that creates meta boxes, i added tome default code into the "value" of the meta box.
The data is being sent outside. my problem is that now i need to press "update" on every woocommerce product for it to be permanently saved and sent. But i have 1500 products.
I have tried bulk updating from the general admin area, like Shawn here:
https://bobwp.com/bulk-edit-posts-wordpress/
It didn't work. so i tried this code:
$args = array(
'posts_per_page' => 1000,
'post_type' => 'product'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$casa_id = $post->ID;
update_post_meta($post->ID, 'casa_id', $casa_id);
}
}
And also this:
function woo_zap_update_product_name() {
if ( ! is_admin() ) {
return;
}
$zap_query = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => -1 ) );
if ( $zap_query->have_posts() ) {
global $post;
while ( $zap_query->have_posts() ) {
$zap_query->the_post();
update_post_meta( $post->ID, '_wc_zap_product_name', $post->post_title );
}
wp_reset_postdata();
}
}
add_action( 'init', 'woo_zap_update_product_name' );
Didn't work as well.. any other ideas ?
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' );
I am currently looping through all posts and displaying a post_meta value like this:
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
foreach ( $getLowestPrice as $post ){
get_post_meta( $post->post_id, '_wholesale_price', false );
}
Is there a way to order the results, lowest -> highest? At the moment they are getting displayed randomly, or as they were entered.
use the following code
<?php
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
$all_post = array();
foreach ( $getLowestPrice as $post ){
$all_post[] = $post->post_id;
}
$query = new WP_Query( array( 'post__in' => $all_post, 'orderby' => 'meta_value', 'meta_key' => '_wholesale_price','order' => 'ASC') );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div>' . get_post_meta( $get_the_ID(), '_wholesale_price', false );() . '</div>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>`
This looks a bit wrong to me. If you're trying to order posts by the value of a meta key (unless I'm mistaken, that is what you're doing) then I would normally go about it using WP_Query()
global $wp_query;
$args = array(
'post_type' => '[YOUR POST TYPE]',
'meta_key' => '_wholesale_price',
'orderby' => 'meta_value meta_value_num', // meta_value_num for value
'order' => 'ASC' // or DSC for low/high
);
$wp_query - new WP_Query( $args );
if ( $wp_query->have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
// Your loop
}
}
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 ) );
}