• Any WordPress experts in here who can tell me why the below code will not work, it does not add the post to the selected category. The category has been checked and has correct id.

    // Create post object
                $my_post = array(
                'post_title' => strip_tags($cp_auction_title),
                'post_type' => 'auction',
                'post_content' => strip_tags($cp_auction_description),
                'post_status' => 'draft',
                'comment_status' => 'closed',
                'post_author' => $current_user->ID,
                'post_category' => array(422)
                );
    
                // Insert the post into the database
                $pid = wp_insert_post( $my_post );
    
                update_post_meta($pid,'closed','0');
    
                update_post_meta($pid,'shipping',$shipping);
                update_post_meta($pid,'paid','0');
                update_post_meta($pid,'location',$location);
                update_post_meta($pid,'buy_now',$buy_now);
                update_post_meta($pid,'start_price',$start_price);
    
                $_SESSION['my_pid_pid'] = $pid;
                wp_redirect(get_bloginfo('siteurl').'/?_post_new_=1&step=2');

    Thanks
    Rune

Viewing 15 replies - 1 through 15 (of 17 total)
  • Hmm, that code looks right on quick inspection, however, because you are using a custom-post type, you should also check to make sure you have registered the custom post type auction correctly in your functions.php, ie., you have something along the lines of:

    'taxonomies' => array('category', 'post_tag')

    As part of the function that you use to register the custom post type?

    Thread Starter ArcticFritid

    (@metuza)

    Yes i think it is wierd..

    This is what i have of function:
    register_taxonomy( 'auctions', 'auction', array( 'hierarchical' => true, 'label' => __('Auction Categories','auctionPlugin') ) );

    Should that do the job which you pointing to?

    Thanks

    Thread Starter ArcticFritid

    (@metuza)

    Forgot his:
    register_taxonomy_for_object_type('post_tag', 'auction');

    I am not sure if “register_taxonomy” also registers the custom taxonomy to your custom post object type… but I imagine it probably does, so that should be good.

    Do you mind posting your register_post_type function from your functions.php?

    Thread Starter ArcticFritid

    (@metuza)

    This is my complete function:

    function cp_auction_plugin_create_post_type() {
    
      $icn = cp_auction_plugin_url()."/images/cpauctionicon.gif";
    
      register_post_type( 'auction',
        array(
          'labels' => array(
            'name' 			=> __( 'Auctions','auctionPlugin' ),
            'singular_name' => __( 'Auction','auctionPlugin' ),
    		'add_new' 		=> __('Add New Auction','auctionPlugin'),
    		'new_item' 		=> __('New Auction','auctionPlugin'),
    		'edit_item'		=> __('Edit Auction','auctionPlugin'),
    		'add_new_item' 	=> __('Add New Auction','auctionPlugin'),
    		'search_items' 	=> __('Search Auctions','auctionPlugin'),
    
          ),
          'public' => true,
    	  'supports' => array('title','editor','author','excerpt','trackbacks','comments'),
    	  'menu_position' => 5,
    	  'register_meta_box_cb' => 'cp_auction_plugin_set_metaboxes',
    	  'rewrite' => array("slug" => "auction" ,'with_front' => false),
    	  '_builtin' => false,
    	  'publicly_queryable' => true,
    	  'hierarchical' => false ,
    	  'menu_icon' => $icn,
    	  'has_archive' => "auctions"
    
        )
      );
    
    	register_taxonomy( 'auctions', 'auction', array( 'hierarchical' => true, 'label' => __('Auction Categories','auctionPlugin') ) );
    	register_taxonomy_for_object_type('post_tag', 'auction');
    }

    Thanks..

    register_taxonomy_for_object_type('post_tag', 'auction');

    will just allow you to use regular vanilla WordPress post_tags with your custom post type “auction”. I am thinking you may also need:

    register_taxonomy_for_object_type('auctions', 'auction');

    on the assumption that register_taxonomy does not register the taxonomy to the the custom post type.

    Yeah, of coure there are 16 ways to skin a cat in WordPress, so your may way be right. I would have done it like this:

    function cp_auction_plugin_create_post_type() {
    
      $icn = cp_auction_plugin_url()."/images/cpauctionicon.gif";
    
      register_post_type( 'auction',
        array(
          'labels' => array(
            'name' 			=> __( 'Auctions','auctionPlugin' ),
            'singular_name' => __( 'Auction','auctionPlugin' ),
    		'add_new' 		=> __('Add New Auction','auctionPlugin'),
    		'new_item' 		=> __('New Auction','auctionPlugin'),
    		'edit_item'		=> __('Edit Auction','auctionPlugin'),
    		'add_new_item' 	=> __('Add New Auction','auctionPlugin'),
    		'search_items' 	=> __('Search Auctions','auctionPlugin'),
    
          ),
          'public' => true,
    	  'supports' => array('title','editor','author','excerpt','trackbacks','comments'),
    	  'menu_position' => 5,
    	  'register_meta_box_cb' => 'cp_auction_plugin_set_metaboxes',
    	  'rewrite' => array("slug" => "auction" ,'with_front' => false),
    	  '_builtin' => false,
    	  'publicly_queryable' => true,
    	  'hierarchical' => false ,
    	  'menu_icon' => $icn,
    	  'has_archive' => "auctions,
             'taxonomies' => array ( 'auctions', 'post-type' )" // I added this line...
        )
      );

    and then register the taxonomies separately as you have done…

    register_taxonomy( 'auctions', 'auction', array( 'hierarchical' => true, 'label' => __('Auction Categories','auctionPlugin') ) );

    But not in the function cp_auction_plugin_create_post_type() function, just as a lose call in functions.php.

    Thread Starter ArcticFritid

    (@metuza)

    Do you mean just like this:

    function cp_auction_plugin_create_post_type() {
    
      $icn = cp_auction_plugin_url()."/images/cpauctionicon.gif";
    
      register_post_type( 'auction',
        array(
          'labels' => array(
            'name' 			=> __( 'Auctions','auctionPlugin' ),
            'singular_name' => __( 'Auction','auctionPlugin' ),
    		'add_new' 		=> __('Add New Auction','auctionPlugin'),
    		'new_item' 		=> __('New Auction','auctionPlugin'),
    		'edit_item'		=> __('Edit Auction','auctionPlugin'),
    		'add_new_item' 	=> __('Add New Auction','auctionPlugin'),
    		'search_items' 	=> __('Search Auctions','auctionPlugin'),
    
          ),
          'public' => true,
    	  'supports' => array('title','editor','author','excerpt','trackbacks','comments'),
    	  'menu_position' => 5,
    	  'register_meta_box_cb' => 'cp_auction_plugin_set_metaboxes',
    	  'rewrite' => array("slug" => "auction" ,'with_front' => false),
    	  '_builtin' => false,
    	  'publicly_queryable' => true,
    	  'hierarchical' => false ,
    	  'menu_icon' => $icn,
    	  'has_archive' => "auctions,
             'taxonomies' => array ( 'auctions', 'post-type' )" // I added this line...
        )
      );
    }
    
    	register_taxonomy( 'auctions', 'auction', array( 'hierarchical' => true, 'label' => __('Auction Categories','auctionPlugin') ) );
    	register_taxonomy_for_object_type('post_tag', 'auction');

    Yes, that’s it… Just a suggestion…

    Thread Starter ArcticFritid

    (@metuza)

    Are you able to see anything wrong here, i get just a blank page:

    function cp_auction_plugin_create_post_type() {
    
      $icn = cp_auction_plugin_url()."/images/cpauctionicon.gif";
    
      register_post_type( 'auction',
        array(
          'labels' => array(
            'name' 			=> __( 'Auctions','auctionPlugin' ),
            'singular_name' => __( 'Auction','auctionPlugin' ),
    		'add_new' 		=> __('Add New Auction','auctionPlugin'),
    		'new_item' 		=> __('New Auction','auctionPlugin'),
    		'edit_item'		=> __('Edit Auction','auctionPlugin'),
    		'add_new_item' 	=> __('Add New Auction','auctionPlugin'),
    		'search_items' 	=> __('Search Auctions','auctionPlugin'),
    
          ),
          'public' => true,
    	  'supports' => array('title','editor','author','excerpt','trackbacks','comments'),
    	  'menu_position' => 5,
    	  'register_meta_box_cb' => 'cp_auction_plugin_set_metaboxes',
    	  'rewrite' => array("slug" => "auction" ,'with_front' => false),
    	  '_builtin' => false,
    	  'publicly_queryable' => true,
    	  'hierarchical' => false ,
    	  'menu_icon' => $icn,
    	  'has_archive' => "auctions",
              'taxonomies' => array('auctions','post-type'))
      );
    }
    
    	register_taxonomy( 'auctions', 'auction', array( 'hierarchical' => true, 'label' => __('Auction Categories','auctionPlugin') ) );
    	register_taxonomy_for_object_type('post_tag', 'auction');
    Thread Starter ArcticFritid

    (@metuza)

    This line crash my site:

    register_taxonomy( 'auctions', 'auction', array( 'hierarchical' => true, 'label' => __('Auction Categories','auctionPlugin') ) );

    What is the error you get in the logs?

    Thread Starter ArcticFritid

    (@metuza)

    Hmm, brand new install, no error logs created…

    Well. i moved that line back into function so back online..

    It works from admin backend, i can add category to post in quick edit.

    Thread Starter ArcticFritid

    (@metuza)

    Maybe i just have to use wp_set_post_terms or something, seems like no one around are able to figure it out..

    Thread Starter ArcticFritid

    (@metuza)

    This fixed the issue šŸ™‚

    // Create post object
      				$my_post = array(
         				'post_title' => strip_tags($cp_auction_title),
         				'post_type' => 'auction',
         				'post_content' => strip_tags($cp_auction_description),
         				'post_status' => 'draft',
         				'comment_status' => 'closed',
         				'post_author' => $current_user->ID
      				);
    
    				// Insert the post into the database
      				$pid = wp_insert_post( $my_post );
    
    				//-------------
    
    				$cp_auction_category = $_POST['cp_auction_category'];
    				$term = get_term( $cp_auction_category, 'auctions' );
    
    				wp_set_object_terms($pid, array($term->slug),'auctions');
    
    				//------------
Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Any WordPress experts in here who can tell me why the below code will not work,’ is closed to new replies.