2

What I want to do is assign a hierarchical term to a custom post type:

function create_frontles_posts() {
  $x = 1;

  do {
    $post_id = wp_insert_post(array(
        'comment_status'  =>  'closed',
        'ping_status'   =>  'closed',
        'post_author'   =>  1,
        'post_name'   =>  'Tile'.$x,
        'post_title'    =>  'Tile',
        'post_status'   =>  'publish',
        'post_type'   =>  'frontiles',
      ));
wp_set_object_terms($post_id, array('mosaic-home'), 'tiles_categories', true);


    $x++;
  } while ($x <= 24);
}

I achieve making automatically that 24 custom posts but no way to assign them the term in that process. Before, I created the term with this function, with no problem:

function example_insert_category() {
  wp_insert_term(
    'Mosaic - Home',
    'tiles_categories',
    array(
      'description' => 'Add Tiles here to load in first term',
      'slug'    => 'mosaic-home'
    )
  );
}
  add_action('init','example_insert_category');

What I'm doing wrong?

2
  • I believe this has issue is related to wordpress.stackexchange.com/questions/18236/…
    – Karun
    Commented Dec 15, 2014 at 18:18
  • Thanks Karun but I can't see the solution in that question. I need this to work..., my knowlodge of php is basic..., I struggle for learn. Sometimes I get lost in this things
    – Dedalos01
    Commented Dec 16, 2014 at 11:44

2 Answers 2

2

I find out the problem and the solution. I debugged the "wp_set_object_terms" by using "is_wp_error" and I got "Invalid Taxonomy" message, then I realized that when the posts was being created that term didn't exists. So I change the hook to "init" in the programmatically_create_post() function, and voila!

Below this line the code working:

    <?php
// TO DO WHEN THEME IS ACTIVATED ///////////////////////
if (isset($_GET['activated']) && is_admin()){

  // 3. Add term "mosaic-home" to custom taxonomy "tiles_categories"
    function example_insert_category() {
      wp_insert_term(
        'Mosaic - Home',
        'tiles_categories',
        array(
          'description' => 'Add Tiles here to load in first term',
          'slug'    => 'mosaic-home'
          )
        );
      }
    add_action('init','example_insert_category');

  // 4. Make loop for creating 24 posts
    function create_frontles_posts() {
      $x = 1;

      do {
        $post_id = wp_insert_post(array(
            'comment_status'  =>  'closed',
            'ping_status'   =>  'closed',
            'post_author'   =>  1,
            'post_name'   =>  'tile'.$x,
            'post_title'    =>  'Tile',
            'post_status'   =>  'publish',
            'post_type'   =>  'frontiles',
            // 'tax_input' =>   array('tiles_categories' => 2),
          ));
          wp_set_object_terms($post_id, 'mosaic-home', 'tiles_categories', true);

            $x++;
          } while ($x <= 24);
        }


// 5. add the loop to the function for create posts
  function programmatically_create_post() {

    // Initialize the page ID to -1. This indicates no action has been taken.
    $post_id = -1;
    $title='';
    // If the page doesn't already exist, then create it
    if( null == get_page_by_title( $title ) ) {
        create_frontles_posts();
        } else {
              // Otherwise, we'll stop
              $post_id = -2;
      } 
    } 
    add_filter( 'init', 'programmatically_create_post' );

} // end to do on activation
?>
1

Here is an example. I've tried to explain each process happening in each line. I hope this code can easily explain you how this is working.

<?php 
//creating a blank array to store the inserted terms ids
$terms = array();

//inserting the term "Kathmandu" in a custom taxonomy "region"
$tax_insert_id = wp_insert_term('Kathmandu','region' );

//if the term "Kathmandu" is inserted successfully, its term_id is returned and stored in $tax_insert_id
//the returned term_id is pushed in the array $terms
$terms[] = $tax_insert_id['term_id'];

//inserting the term "Banepa" in a custom taxonomy "region"
$tax_insert_id = wp_insert_term('Banepa','region' );

//if the term "Banepa" is inserted successfully, its term_id is returned and stored in $tax_insert_id
//the returned term_id is pushed in the array $terms
$terms[] = $tax_insert_id['term_id'];

//Creating a post array
$post = array(
    'post_title'      => 'Title',
    'post_content'      => 'This is a dummy text',
    'post_status'    => 'publish',
    'post_type'      => 'post',
);

//Inserting the post in WordPress using wp_insert_post()
//if the post is successfully posted, post_id is returned and stored in $the_post_id
$the_post_id = wp_insert_post( $post );

//assign the terms stored in $terms array to $the_post_id post
wp_set_post_terms( $the_post_id, $terms, 'region' );
3
  • Thanks Karun! but I'm trying to test it and have this php error "Fatal error: Cannot use object of type WP_Error as array in..." in this line: $terms[] = $tax_insert_id['term_id'];
    – Dedalos01
    Commented Dec 16, 2014 at 17:42
  • You can check out how it looks now the whole code here: pastebin.com/embed_iframe.php?i=gxp71NLt
    – Dedalos01
    Commented Dec 16, 2014 at 17:51
  • It works fine. But can u little elaborate that how to create multiple posts under different term?
    – Shiplu
    Commented Apr 29, 2022 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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