Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using this custom function to add a publish/unpublish button to the front end of my site for the users:

    if ('draft' == get_post_status ($post_id)) {
            $post = get_post($post_id,ARRAY_A);
            $post['post_status'] ='publish';
            wp_insert_post($post);
            }


        else if ('publish' == get_post_status ($post_id)) {
            $post = get_post($post_id,ARRAY_A);
            $post['post_status'] ='draft'; 
            wp_update_post($post);
            }

But, for some reason, the category of the post is changing to the parent category of the post instead of sticking with the actual category of the post.

For instance, I have 5 categories with 1 parent, like so:

ParentCat
 - ChildCat1
 - ChildCat2
 - ChildCat3
 - ChildCat4
 - ChildCat5

Each post from the front end is entered into one of the five categories. When I click the "Publish" button that I've created, it changes the category to "ParentCat" instead of keeping it as the child category.

Anyone know how I can fix this?

share|improve this question
    
IIRC when you assign a post to a subcategory, it fetches the entire chain. So the first record would be the parent in this case - try to debug the $postcat array, probably the second element is the actual child category that you need. –  Mario Peshev Nov 10 '12 at 14:30
    
Thanks for responding. It seems like I could fix it simply be making all the categories parents instead of children. That might be a quick fix. I tried using the function without any category parameters to see if it would just do it by itself, but no-go. I'm also wondering if I could somehow set the parent category automatically when the post is orginally submitted and then just display the child categories on the front end (basically hide the parent one). Hmmmm... I'll comment back when I get a solution in place. –  Eckstein Nov 12 '12 at 3:35
    
To clarify that last comment: I'm not sure how to set the parent category automatically from the frontend form. Right now it's only submitting the child category whereas the publish link (on their frontend dashboard page) is only submitting the parent category. –  Eckstein Nov 12 '12 at 3:46
    
There shouldn't be any substantial difference there, your publish code should get a category ID passed so should that be a parent category, it would always push that to the parent, right? –  Mario Peshev Nov 12 '12 at 10:18
    
For some reason when I originally publish the post from the front end, it does NOT set the parent category as well. It only sets the child category. I think this might be where the problem lies. If it assigned both, I think they would stick. –  Eckstein Nov 12 '12 at 16:59

1 Answer 1

up vote 1 down vote accepted

I figured it out! Apparently categories are no longer part of the database table for posts and are instead stored elsewhere. Therefore using the wp_insert_post has no way of setting a category.

I had to use wp_set_post_terms in order to set the categories which worked to automatically set both the parent and the child at the same time upon publishing.

wp_update_post doesn't seem to mess with the categories, so there's no need to pass them there.

Here is the part of the now working function:

$category=get_the_category ($post_id);
    $categories=array ('89', $category[0]->cat_ID);

    //Publish or unpublish post
        if ('draft' == get_post_status ($post_id)) {
            $post = get_post($post_id,ARRAY_A);
            $post['post_status'] ='publish';
            wp_insert_post($post);
            wp_set_post_terms( $post_id, $categories, 'category' );
            }


        else if ('publish' == get_post_status ($post_id)) {
            $post = get_post($post_id,ARRAY_A);
            $post['post_status'] ='draft'; 
            wp_update_post($post);
            }

I hope this is helpful to someone!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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