1

I have a CPT called (services) that has a custom field with name of (price), and there is also a custom taxonomy which has a CPT with a name (catalog) that has also a custom field with a name of (price).

it is meant for a catalog post to have it's service price, and i already added the services prices. Now i want to bulk copy the (price) custom field in services to the (price) custom field in (Catalog) in bulk, How can i do this?

To be mentioned, posts has the same names

1

Your problem is pretty complex. Without getting into your system it's pretty tough to solve it. For your case I would prefer to run database query but it's seemed too complex to me. By the way here I've tried to write a simple PHP script-

function the_dramatist_get_post_by_title( $page_title, $post_type ='post' ) {
    global $wpdb;
    $post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type));
    if ( $post )
        return $post;

    return null;
}

function the_dramatist_bulk_copy_c_fields() {
    $c_posts = wp_list_pluck( get_posts(array(
        'post_type'=>'catalog',
        'posts_per_page' => -1
    )), 'ID' );

    foreach($c_posts as $c){
        $title = get_the_title( $c );
        $s_id = the_dramatist_get_post_by_title( $title, 'services');
        $meta_value = get_post_meta( $s_id, 'price', true );
        add_post_meta( $c, 'price', $meta_value );
    }
    return true;
}

add_action('init', 'the_dramatist_bulk_copy_c_fields');

Run the script only once. I haven't got the chance to test it. So please test it before using it in production.

Hope that helps.

  • Thanks @the_dramatist, but unfortunately it didn't work – Mohamed Omar Jan 6 '17 at 11:13
  • you answer makes sense and i don't know why it didn't work , i had to change the post type name to the real ones td_catalog and td_services but nothing changed – Mohamed Omar Jan 6 '17 at 11:27
  • @MohamedOmar, Can you please share your database ? I'm planning to write a SQL query to do your job. Without seeing your database it's pretty tough. So I need your database. – CodeMascot Jan 6 '17 at 16:58
  • Here is a download link of database (works on localhost): mediafire.com/?3vxbbvdszy0xh5q – Mohamed Omar Jan 7 '17 at 9:02
  • please use the product post type with catalog and forget about services . the product also has the same name – Mohamed Omar Jan 7 '17 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.