• REST API returns wrong existing category ID disregarding ParentID on new category adding.

    Sample categories (PID/ID/Name):
    0/2/Apple
    0/3/Test

    $req = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    $req->set_param( 'parent', 3 );
    $req->set_param( 'name', 'Apple' );
    $r = rest_do_request( $req );
    // returns $r->data['data']==2 

    Possible solution:
    edit taxonomy.php and insert code

    if ( $name_match->slug === $slug && in_array( $name, wp_list_pluck( $siblings, 'name' ) ) ) {
    // insert start
              foreach ( $name_matches as $_match ) {
                if ( (strtolower( $name ) === strtolower( $_match->name )) && $_match->parent==$parent) {
                  $name_match = $_match;
                  break;
                }
              }
    // insert end   
    $existing_term = $name_match;
Viewing 1 replies (of 1 total)
  • Thread Starter esvon

    (@esvon)

    Some clarification is needed.
    The error can’t be reproduced with the categories specified above. I had a complex category tree with 2 “Apple” subcategories with different parent IDs.
    Example (ID-Category):
    1-Hardware > 3-Apple
    2-Software > 4-Apple

    When trying to add already existing subcategory “Apple” under “Software” category ID under “Hardware” is returned (i.e. first matching subcategory with name “Apple”)

    $req = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    $req->set_param( 'parent', 2 );
    $req->set_param( 'name', 'Apple' );
    $r = rest_do_request( $req );
    // returns $r->data['data']==3
Viewing 1 replies (of 1 total)
  • The topic ‘REST API returns wrong existing category ID disregarding ParentID’ is closed to new replies.