1

The following code in functions.php outputs a post when I publish, not a Page:

// custom post_type for agenda
add_action( 'init', 'create_my_post_types' );    
function create_my_post_types() {
    register_post_type( 'agenda', array(
        'labels' => array(
            'name_admin_bar' => _x( 'Agenda', 'add new on admin bar' ),
        ),
        'public' => true,
        'publicly_queryable' => false,
        'capability_type' => 'page',
        'map_meta_cap' => true,
        'hierarchical' => true,
        'rewrite' => false,
        'query_var' => false,
        'delete_with_user' => true,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
    ) );}

What am I doing wrong?

The full problem:

I need to add a metabox to a page, but only the "agenda" page, I don't want this metabox to appear in every page in admin panel, so I'm creating a custom post_type with the exact same functions of a page, only with a different post_type to be able to set a metabox to it alone.

Screenshot of current output:

https://i.sstatic.net/oPEx8.png (can't post direct images yet, so please click link)

2 Answers 2

2

Your screenshot seems to indicate that you're interested in the status message for your custom post type saying 'Page updated. View page'. If that's true, the reason that you're getting 'Post updated. View post' is that you haven't provided status messages for your custom post type.

Here's the code for the admin edit page (edit-form-advanced.php):

if ( isset($_GET['message']) ) {
    $_GET['message'] = absint( $_GET['message'] );
    if ( isset($messages[$post_type][$_GET['message']]) )
        $message = $messages[$post_type][$_GET['message']];
    elseif ( !isset($messages[$post_type]) && isset($messages['post'][$_GET['message']]) )
        $message = $messages['post'][$_GET['message']];
}

So the edit screen is trying to retrieve messages for your $post_type (agenda), not finding them, and displaying the post messages.

You can fix this by creating $messages['agenda'] with something like the approach described on the Register Post Type function reference:

function agenda_update_messages( $messages ) {
  global $post, $post_ID;
  $messages['agenda'] = array(
    // ...
    1 => sprintf( __('Page updated. <a href="%s">View page</a>', 'your_text_domain'),
                  esc_url( get_permalink($post_ID) ) ),
    // ...
  ); 
  return $messages;
}
add_filter( 'post_updated_messages', 'agenda_update_messages' );
0

You Don't need to create a custom post type just add the metabox if the page is the one you require ex:

add_action('admin_init','meta_on_agenda_only');

function meta_on_agenda_only(){
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;

    // checks for post/page ID change the 00 to the ID pf your agenda page
    if ($post_id == '00' || is_page('agenda'))  {
        //add your metabox
        add_meta_box('meta_box_id', 'Meta Box name', 'callback_function', 'page', 'normal', 'high');
    }
    //hook the save action
    add_action('save_post','my_meta_save');

}
4
  • Bainternet, thanks for your time, but unfortunately that won't work for me. I have a startup company that make websites for end-users to edit, based on wordpress, so it will be a dynamic ID for each website... I'm brazilian, and the ppl who's going to use these websites have a really limited tech knowledge, so I thought of doing that way to keep it simplier as possible ... I'm pretty broken right now, but I'm hoping this project will help me bounce back! Commented Apr 8, 2013 at 9:07
  • Neverthless, this is a question that can be used in thousands of projects, not only mine... I've googled a lot and found a lot of questions, but no concrete answer about this :) Commented Apr 8, 2013 at 9:08
  • the ID is dynamic but the slug can be the same? its just makes no sense to create a post type just for a single page. if you want another solution, you can use custom field to determine if the metabox should be added or not
    – Bainternet
    Commented Apr 8, 2013 at 9:11
  • I've searched about custom_field and found an very interesting plugin: Advanced Custom Fields; However, that still didn't seem to solve my problem. The only problem I'm having right now that I really need help is that the code above outputs a POST, instead of a PAGE, and it must output a PAGE for reasons... Thanks for your time so far, any ideas? Commented Apr 8, 2013 at 16:32

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.