• Resolved pienoz

    (@pienoz)


    import post by program.
    I use :

    $term[] = “sport »;
    
    $my_post = array(
    post_title’ => $title_headline,
    post_content’ => $body,
    post_excerpt’ => $postExcerpt,
    ‘post_status’ => ‘publish’,
    ‘post_category’ => $term,
    ‘post_type’ => ‘post’,
    post_author’ => $userID,
    ‘tax_input’ => array(‘category’ => $term),
    ‘post_date’ => date( ‘Y-m-d H:i:s’, $thisrevisioncreated )
    );
    $new_post = wp_insert_post($my_post);

    The post is created, but taxonomy is not attached to the post. Why ?

    • This topic was modified 3 months, 3 weeks ago by bcworkz. Reason: code format fixed
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Because the category taxonomy is hierarchical, you must use an array of term IDs as the “tax_input” value. Names are not acceptable because category terms can have the same name through differing ancestry. On this doc page, scroll down to the “tax_input” arg.

    The quotes you use in your code as posted here are improper for proper PHP syntax (and some are missing). I suspect your original code is OK, something got corrupted in the copy/paste process. I did edit your topic to use the code block instead of a citation block, but the code was already corrupted. I made no attempt to correct it.

    Anytime you post code in these forums, please use the code block. Doing so minimizes the chance your code will become corrupted.

    Thread Starter pienoz

    (@pienoz)

    Hello,

    It’s work with ID.

    But why it’s work with (“sport” is the slug):

    $term[] = "sport";
    wp_set_object_terms($new_post, $term, 'category');

    Thread Starter pienoz

    (@pienoz)

    Strange behavior:
    If I launch the import of articles via the back-office, the categories are correctly attached. But if the program runs automatically from the cron, then the default category is always attached to the article, and not the one I want.

    Moderator bcworkz

    (@bcworkz)

    I suppose it could work with a slug where there’s no redundancy, but it’s Doing It Wrong® since the possibility of redundancy exists.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.