848 posts
  • Referred between 1 and 9 users
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Lebanon
  • Exclusive Author
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
  • Exclusive Author
  • Has been a member for 1-2 years
  • Referred between 10 and 49 users
  • Bought between 1 and 9 items
  • Sold between 10 000 and 50 000 dollars
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); 
       }
848 posts
  • Referred between 1 and 9 users
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Lebanon
  • Exclusive Author
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
  • Exclusive Author
  • Has been a member for 1-2 years
  • Referred between 10 and 49 users
  • Bought between 1 and 9 items
  • Sold between 10 000 and 50 000 dollars
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

848 posts
  • Referred between 1 and 9 users
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Lebanon
  • Exclusive Author
MoeHaydar says

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

983 posts It's in the pipeline!
  • Elite Author
  • Has been a member for 5-6 years
  • Bought between 100 and 499 items
  • Referred between 200 and 499 users
  • Exclusive Author
  • Envato Studio (Microlancer) Beta Tester
  • Author had a Free File of the Month
+2 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.

133 posts
  • Sold between 10 000 and 50 000 dollars
  • Most Wanted Bounty Winner
  • Referred between 10 and 49 users
  • Bought between 10 and 49 items
  • Grew a moustache for the Envato Movember competition
  • Has been a member for 5-6 years
  • Exclusive Author
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.

848 posts
  • Referred between 1 and 9 users
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Lebanon
  • Exclusive Author
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