0

I have just made a big mistake today using a plugin to convert my 1000+ price custom fields into child categories under Price. I didn't know the custom fields will be deleted after the conversion.

The prices are still attach to their respective posts and I'll really appreciate it if somebody can help me with sql query to copy the values back to the price custom field.

this is my function.php including your code

<?php
//get all categories ID's in to array
$args = array( 'child_of'  =>3678);
$categories = get_categories( $args );
$cat_array = array();
foreach ($categories as $category) {
    $cat_array[] = $category->term_id;
  }

$Query_args = array(
    'category__in' =>$cat_array );
$new_query = new WP_Query();
$new_query->query($Query_args);
if ($new_query->have_posts()){
    while ($new_query->have_posts()){
        $new_query->the_post();
        foreach((get_the_category()) as $category) { 
            if (in_array($category->cat_ID,$cat_array){
                update_post_meta($post_id, 'price', $category->cat_name);
            }
        }
    }
}


/*************************************************************
* Do not modify unless you know what you're doing, SERIOUSLY!
*************************************************************/

/* Admin framework version 2.0 by Zeljan Topic */

// Theme variables
require_once (TEMPLATEPATH . '/library/functions/theme_variables.php');

//** ADMINISTRATION FILES **//

// Theme admin functions
require_once ($functions_path . 'admin_functions.php');

// Theme admin options
require_once ($functions_path . 'admin_options.php');

// Theme admin Settings
require_once ($functions_path . 'admin_settings.php');



//** FRONT-END FILES **//

// Widgets
require_once ($functions_path . 'widgets_functions.php');

// Comments
require_once ($functions_path . 'comments_functions.php');

// Yoast's plugins
require_once ($functions_path . 'yoast-breadcrumbs.php');

require_once ($functions_path . 'yoast-posts.php');

//require_once ($functions_path . 'yoast-canonical.php');

require_once ($functions_path . 'yoast-breadcrumbs.php');

/////////shopping cart new function files
require($functions_path . "general_functions.php");
require($functions_path . "cart.php");
require($functions_path . "product.php");
require($functions_path . "custom.php");
require(TEMPLATEPATH . "/product_menu.php");
// Custom
require_once ($functions_path . 'custom_functions.php');

///message - language file
require(TEMPLATEPATH . "/message.php");
if('themes.php' == basename($_SERVER['SCRIPT_FILENAME'])  && $_REQUEST['page']=='') 
{
    if($_REQUEST['dummy']=='del')
    {
        delete_dummy_data();    
        echo THEME_DUMMY_DELETE_MESSAGE;
    }
    $post_counts = $wpdb->get_var("select count(post_id) from $wpdb->postmeta where meta_key='pt_dummy_content'");
    if(($_REQUEST['template']=='' && $post_counts>0 && $_REQUEST['page']=='') || $_REQUEST['activated']=='true')
    {
        echo THEME_ACTIVE_MESSAGE;
    }
    if($_REQUEST['activated'])
    {
        require_once (TEMPLATEPATH . '/auto_install.php');
    }
}

function delete_dummy_data()
{
    global $wpdb;
    $productArray = array();
    $pids_sql = "select p.ID from $wpdb->posts p join $wpdb->postmeta pm on pm.post_id=p.ID where meta_key='pt_dummy_content' and meta_value=1";
    $pids_info = $wpdb->get_results($pids_sql);
    foreach($pids_info as $pids_info_obj)
    {
        wp_delete_post($pids_info_obj->ID);
    }
}

?>

1 Answer 1

2

paste this code in you theme's function.php file

//get all categorie ID's in to array
$args = array( 'child_of'  => 3678); // Parent category ID
$categories = get_categories( $args );
$cat_array = array();
foreach ($categories as $category) {
    $cat_array[] = $category->term_id;
  }

$Query_args = array(
    'category__in' => $cat_array,
    'post_type' => 'YOUR_POST_TYPE_NAME'
);
$new_query = new WP_Query();
$new_query->query($Query_args);
if ($new_query->have_posts()){
    while ($new_query->have_posts()){
        $new_query->the_post();
        foreach((get_the_category()) as $category) { 
            if (in_array($category->cat_ID,$cat_array)){
                update_post_meta($post_id, 'price', $category->cat_name);
            }
        }
    }
}

replace "Parent category ID" with price category ID and then save that file after that remove this code and save again.

10
  • Thanks for that Bainternet but I'm getting this error: Parse error: syntax error, unexpected '{' on line 18 .... I had placed the code below <?php and replaced the parent category with price id...I've been trying for the past hour but I just couldn't find it..
    – Mars
    Commented Feb 26, 2011 at 20:17
  • 1
    paste the code that you are using?
    – Bainternet
    Commented Feb 26, 2011 at 20:35
  • Hi Bainternet, I've posted the code above...
    – Mars
    Commented Feb 27, 2011 at 13:34
  • i see its missing a frikin ")" at the end of if (in_array($category->cat_ID,$cat_array) . I updated the code in the answer so just copy it and replace with the one in your functions.php
    – Bainternet
    Commented Feb 27, 2011 at 14:08
  • 1
    if you are using custom post type the you should have mentioned it, anyway add 'post_type' => 'YOUR_POST_TYPE_NAME' to the $Query_args array
    – Bainternet
    Commented Feb 27, 2011 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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