887 posts
  • Pulled off some great moustache to raise money or awareness for Movember Australia
  • Made it to the Authors' Hall of Fame
  • Has sold $40,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+6 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!

45 posts
  • Has sold $40,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
+3 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); 
       }
887 posts
  • Pulled off some great moustache to raise money or awareness for Movember Australia
  • Made it to the Authors' Hall of Fame
  • Has sold $40,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+6 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 ?

45 posts
  • Has sold $40,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
+3 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

887 posts
  • Pulled off some great moustache to raise money or awareness for Movember Australia
  • Made it to the Authors' Hall of Fame
  • Has sold $40,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+6 more
MoeHaydar says

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

1058 posts It's in the pipeline!
  • Has sold $500,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
  • Elite Author: Sold more than $75,000 on Envato Market
  • 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.

272 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
+5 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.

887 posts
  • Pulled off some great moustache to raise money or awareness for Movember Australia
  • Made it to the Authors' Hall of Fame
  • Has sold $40,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+6 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

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by