Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6919153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:57:13+00:00 2026-05-27T09:57:13+00:00

I set up a custom content type called photo which is working fine. Now

  • 0

I set up a custom content type called “photo” which is working fine. Now I’m inserting posts with that content type using wp_insert_post() and am encountering a problem adding a category. These are hierarchical categories that I associated with the post type and can set in the admin area, but I try to associate them using PHP code, it doesn’t work.

Here is the code snippet:

$new_entry = array();
$new_entry['post_title'] = $_POST['submit-title'];
$new_entry['post_content'] = '';
$new_entry['post_status'] = 'pending';
$new_entry['post_type'] = 'photo';
$new_entry['post_author'] = $userID;
$new_entry['post_category'] = array(6,7); // this is the line that doesn't work
$new_entry['tags_input'] = array('nature','people','fun'); // this also doesn't work

// Insert the post into the database
$post_id = wp_insert_post( $new_entry );

This all works and the custom post is inserted and shows up as expected, but it doesn’t have any categories assigned to it. (Categories 6 and 7 are categories that should be associated with the “photo” post type). More than that the tags are also not created and associated with this post.

Does anyone have any thoughts on what I’m doing wrong or other approaches they recommend?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:57:13+00:00Added an answer on May 27, 2026 at 9:57 am

    There is a function for that, it is called wp_set_object_terms(). http://codex.wordpress.org/Function_Reference/wp_set_object_terms

    eg:

    wp_set_object_terms( $post_id, $category_ids, 'category');
    

    you can use the same function to add tags

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom content type that has a ReferenceField. When I set
Is there a way to set a custom content type page to a custom
I have the following scenario: I have a content type called 'Product' in which
So, I have a custom attribute called CompressAttribute which is set up as a
I want to create a custom set that will automatically convert objects into a
I've defined a content type 'related links' and set Inherits=False and added line to
I have a custom content type with 2 custom fields: file (file) and list
Trying to use features to deploy custom fields, content type and list. So far
I have a custom folderish Dexterity content-type in Plone. It can have only Documents
I'm trying to set a custom order for my term set Cities using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.