I've created a plugin that auto creates a post and attaches a category to it, which is then hidden from the main blog and only shows in the category listing...

I can successfully add a new post via wp_insert_post with all the correct params and once the post_id is returned I'm calling wp_set_object_terms with the category I want that post to be included under.

I can confirm that the post is created, and the post has the correct category. However, if I visit the category listing for that particular cat, I see all the previous posts but not the dynamically created one. If I manually add a new post and select the same category, it does display in the list.

I haven't found anything regarding why this is happening... Has anyone else run into this issue? Everything seems to be working fine, except with the category listing...

(Edited to add code sample)

    $new_post = array(
    'post_title' => $postTitle,
    'post_content' => '$postContent,
    'post_status' => 'publish',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $current_user->ID,
    'post_type' => 'post'

$post_id = wp_insert_post($new_post);

wp_set_object_terms($post_id, array(24), 'category');
  • 2
    do you set post_status? it would help to see the code that inserts the post. – Milo Feb 18 '13 at 0:33
  • code sample added... the post shows up in the post listing with the category added which tells me everything I did was correct. – orderofout Feb 18 '13 at 19:34
  • what does it say in the date column in the admin post list? is it Published, or possibly Scheduled? – Milo Feb 18 '13 at 19:53
  • I've experienced this too when programatically creating posts. For some reason the category/term count are not updated. I think the same goes for imports as well. See this thread for a possible explanation and solution. – Simon Feb 18 '13 at 20:28
  • @milo you hit the nail on the head. It is scheduled for 4 hours from now. Well at least now I know what's causing the issue. If only I could give you credit for pointing that out. Thanks! – orderofout Feb 19 '13 at 14:33

I think you have a typo mistake there line 3.

the code could be like this

    'post_content' => $postContent,

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.