WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Cannot Avoid Creating Duplicate Categories (3 posts)

  1. WebTechGlobal
    Member
    Posted 4 years ago #

    Hi,

    For 2 days I've been working on a bit of code that automatically creates a bunch of categories from csv file. I used to work just fine until recently despite no changes. Now for some reason the script creates the PARENT and CHILD plus links a posts so all is ok.

    Then on the next refresh of the blog the script creates a duplicate of the child category with the slug being extended by the second word of the first child category slug. A post is also put in that duplicate category.

    Then after that all new posts are put in the first child category and the duplicate no longer matters. On a mass scale this is not funny so please understand I'm talking about csv importing plugins.

    Here is my code and I would seriously appreciate anyone poking holes in it...

    $catid = get_cat_ID( $categoryname1 );// returns false if no match found else true

    // make new category if false returned
    if($catid == false)
    {
    if($catparent == 1)// if = 1 all new categories have the set parent from options page
    {
    $my_cat = array('cat_name' => $categoryname1, 'category_parent' => $catparent);
    if($catid == false){$catid = wp_insert_category( $my_cat );}
    }
    elseif($catparent == 0)
    {
    $my_cat = array('cat_name' => $categoryname1);
    if($catid == false){$catid = wp_insert_category( $my_cat );}
    }
    }

    if(isset($catid)){$catidarray['0'] = $catid;}

    ############# CHILD ONE #################
    if( $cam->filtermethod== 'automated' && $cam->filtercolumn2 != 999)
    {
    // if category name does not exist create it

    $arg= array();
    $arg['child_of'] = $catid;
    $arg['hide_empty'] = false;
    $subcategories = get_categories( $arg );

    foreach ($subcategories as $cat)
    {
    if($cat->cat_name == $categoryname2)
    {
    $catid2 = $cat->term_id;
    }
    }

    // make new category if false returned
    if(!isset($catid2) || $catid2 == false)
    {
    $my_cat = array('cat_name' => $categoryname2, 'category_parent' => $catid);
    $catid2 = wp_insert_category( $my_cat );
    }
    }

    if(isset($catid2)){$catidarray['1'] = $catid2;}

    Obviously thats not the whole script but the main area is...

    $subcategories = get_categories( $arg );
    foreach ($subcategories as $cat)

    It is not finding the child for the parent and so allow the foreach loop to run and find that child to get the id.

    Why?!

    Parent ID is passed for sure else it would keep creating duplicates constantly.

    Thanks everyone
    Ryan Bayne

  2. WebTechGlobal
    Member
    Posted 4 years ago #

    I can't believe I just fixed it after debugging for another entire day and coming here for help!

    Replace wp_insert_category with wp_create_category and it works better. I'm not going to look into why right now, but ye why would one cause the child of a parent to not be detected and so a duplicate is created BUT when that duplicate is created the initial child gets used for the new posts!

  3. WYDUmanagement
    Member
    Posted 4 years ago #

    Hey WebTech i just did a large merger from a blogger to a WP setup, whats the chances of getting a copy of the script to create a whole bunch of categories so i do not have to do it manually ??

    its a long shot but i figured if i didn't ask i wouldn't know.

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic