854 posts
  • Has sold $40,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
  • Has been part of the Envato Community for over 5 years
  • Interviewed on an Envato blog
+5 more
MoeHaydar says

Sometimes the easiest things cause the most problems! This is a case:
I’ve spend 4 hours yesterday trying to solve this problem but couldn’t. I am sure I have something missing! Help is appreciated

So I’ve wrote a piece of code that would automatically creates a page. The problem arises when i give this page a custom post type: The page doesn’t show in pages area or when visited.

I’ve made sure that the post type is all lowercase letters. I also verified that the post type is successfully registered.
The insert post runs successfully and no errors are shown in debugging mode!

Am I missing something? I went crazy on this.

Code that creates post type:

add_action( 'init', 'create_whatever' );
function create_whatever() {
    register_post_type( 'whatever',
        array(
            'labels' => array(  'name' => 'Whatever Page',   'singular_name' => 'Whatever Page' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => false,
        'query_var'          => true,
        'has_archive'        => false
        )
    );

}



Code uses to create post:

//not all pages have same array, this is a flexible way to populate it.
$_p = array();
$_p['post_title'] = $this->whatever_title;
$_p['post_content'] = $this->whatever_desc;
$_p['post_status'] = 'publish';
$_p['post_type'] = 'whatever';
$_p['comment_status'] = 'closed';
$_p['ping_status'] = 'closed';
$_p['post_category'] = array(1); 

$page_id = wp_insert_post( $_p );


When post_type is page, everything goes as expected but when i change it to the custom post type. The page doesn’t show up.
Please note that the first code is called before the second. I also tried adding the first code to my themes function rather than the plugin. Same stuff.
Am I missing something so vital here?

Custom Post type will be used just to identify the page and redirect it to a special page template.
I was able to achieve the same effect without page types (I used page id, because I will need this for only one page), the problem here was that the page will apear in the menu and i dont want it to do so. Yes I know we can remove it, but i want a complete solution?



Thanks!

42 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
phppoet says
your code looks okay . Here is what is working for me
if ((empty($error)))  {
            $new_post = array(
            'post_title'    =>    $title,
            'post_content'    =>    $description,
            'tags_input'    =>  array($tags),
            'post_category'    =>    array($_POST['cat']),  
            'post_status'    =>    'publish',          
            'post_type'    =>    'book',  
        );

        //insert post

        if (!isset($pid)) {
        $pid = wp_insert_post($new_post); 
       }
854 posts
  • Has sold $40,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
  • Has been part of the Envato Community for over 5 years
  • Interviewed on an Envato blog
+5 more
MoeHaydar says

That is what is making me go crazy, the code looks okay.
Yours is similar to mine.

My insert code runs on plugin activation.
The post type runs on the init hook. Can this be the problem ?

42 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
phppoet says

no. It should work fine with activation hook . make sure post slug , name exactly matches with post type name in insert post .

you should consider using something like which has worked for me every time

http://pastebin.com/p19amW9K

regards

854 posts
  • Has sold $40,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
  • Has been part of the Envato Community for over 5 years
  • Interviewed on an Envato blog
+5 more
MoeHaydar says

I will try, but i dont think this is the problem Thanks for your help.

993 posts It's in the pipeline!
  • Has sold $500,000+ on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has been part of the Envato Community for over 5 years
  • Sells items exclusively on Envato Market
+7 more
purethemes says

Maybe the create_whatever() runs before the ‘register_post_type’, and if the post type does not exists yet, this post is not added, so try to set some priorities for those functions.

175 posts
  • Has sold $10,000+ on Envato Market
  • Won a Most Wanted contest
  • Made it to the Authors' Hall of Fame
  • Has referred 10+ members
+4 more
GDragoN says

You can’t use plugin activation hook to insert post, it runs too early before init. So, anything that needs post types, must be run after post type is initialized.

854 posts
  • Has sold $40,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
  • Has been part of the Envato Community for over 5 years
  • Interviewed on an Envato blog
+5 more
MoeHaydar says

This is what I thought. This is why i was playing around with what runs where.
I just wanted to get see if it was something else.

Please note that i did try adding a post that has a page post_type. And then after i initialize the type I change the post type to the new one.

I will have to dig more tomorrow.

Thanks for your replies

by
by
by
by
by
by