25 responses to “Thesis Theme: Using WordPress Custom Post Types”

  1. Justin Bartlett

    Hmm for some reason it didn’t add the thesis default meta boxes…

  2. Justin Bartlett

    What a laughable error on my part – I completely missed the first chunk of code; I read your comment – line 45. Wait, there’s no line 45.. What’s going on here??

    Thanks Dimas!

    Actually I’m really having a tough time getting the hang of writing custom posts and inserting fields in these posts… I’m trying to create meta boxes that show up on custom post types. I saw your other tutorial on custom posts + meta boxes but am having a tough time following – do you have a code that I could implement in thesis?

  3. Justin Bartlett

    Will do! Thank you so much! If you have a minute and want to take a look over my code, I attempted to follow the “how to create a wordpress custom meta box” tutorial in conjunction with the “how to limit the display of a meta field” and also tried to incorporate this article –

    here’s my error message:

    Warning: call_user_func(my_meta_setup_3) [function.call-user-func]: First argument is expected to be a valid callback in /home/events/public_html/wp-admin/includes/template.php on line 2868

    & here’s my custom_functions.php code: http://pastie.org/1043751

  4. David Alexander

    Great stuff. This worked perfectly for moi. Stumbled the article. 🙂

  5. Eric Irwin

    I used a plugin called Custom Post Types UI to create my custom post type. The plugin makes it very easy to manage custom post types. I waited to see if Chris added a way to have Thesis meta boxes in 1.8 but he just released it this morning and I’m not seeing the meta boxes. So, I added your code to add the custom meta boxes to my custom post type and it worked out great. Thanks for the tip Dimas!

  6. Raintrader

    I tried your code to add the Thesis Metabxes to my custom Posts. So far it worked like a charm.
    But how can i make non Thesis Metaboxes (from plugins for exp.) show up in my custom post eidt Screen too???

    Any Help Welcome

    Thanx Sascha

  7. Rich

    Hey. Thanks for the tip / code.

    I’ve added the code for the meta boxes on a custom post type on my development site. The meta boxes appear just as expected.

    Unfortunately, changes to any of the added meta boxes do not save.

    I’ve tracked the problem down to the save_meta() function in options_post.php. The data is not saving because it fails the nonce check (lines 146-150 of options_post.php)

      // We have to make sure all new data came from the proper Thesis entry fields
      foreach($post_options->meta_boxes as $meta_box) {
        if (!wp_verify_nonce($_POST[$meta_box['noncename'] . '_noncename'], plugin_basename(__FILE__)))
          return $post_id;
        }
    

    I know it fails there because I commented out that particular section for testing and the data saves fine when it is commented out. I, obviously, want to keep the nonce check in there, for security reasons.

    Any thoughts?
    I have doublechecked and have not strayed from what you have suggested, other than excluding certain meta boxes from displaying based on “$meta_box[‘id’]”
    Which, now that I think about it, is exactly why it is failing.
    The loop in options_post is going through all the default metaboxes
    Since I have prevented some from being displayed, their nonce doesn’t exist on my edit page, so the nonce check fails for those missing boxes.

    So, the question is, how do I display working metaboxes for only those metaboxes I want?

  8. Rich

    I still don’t think that will work. I can easily choose the metaboxes I want to appear or not appear.
    The problem is that in save_meta() in options_post.php, the loop is:

    function save_meta($post_id) {
      $post_options = new thesis_post_options;
      $post_options->meta_boxes();
    
      // We have to make sure all new data came from the proper Thesis entry fields
      foreach($post_options->meta_boxes as $meta_box) {
        if (!wp_verify_nonce($_POST[$meta_box['noncename'] . '_noncename'], plugin_basename(__FILE__)))
          return $post_id;
      }
    ... 
    }
    

    $post_options->meta_boxes doesn’t have anything to do with what I have decided should appear on my edit page. Or am I missing something? Even if I create a custom array (as opposed to skipping the add_meta_box() function call based on the metabox name / id), that custom array doesn’t equal the array that is being looped through in save_meta() in options_post.php.

    From what I understand, $post_options->meta_boxes will contain all of the metaboxes, regardless of what I have decided to display? So, the nonce will fail when the loop in options_post.php is on a metabox in $post_options->meta_boxes that is not in the set of metaboxes I have elected to display on my custom post edit screen.

    If I include ALL metaboxes, as is coded in lines 56-59, the meta information saves fine. It only fails if I decided to exclude anything.

    Unless the scope of $post_options->meta_boxes in save_meta() (options_post.php) is different than I understand?

    As reference, here is the code I am using that does display the metaboxes I want, yet will not save/update the metadata entered into the metaboxes. If I remove the if statement (so, including all metaboxes), the meta data saves fine:

    add_action('admin_menu','guide_admin_menu');
    function guide_admin_menu()
    {
      $post_options = new thesis_post_options;
      $post_options->meta_boxes();
     
      foreach ($post_options->meta_boxes as $meta_name => $meta_box) 
      {
        if ($meta_box['id'] != "thesis_multimedia_meta" && $meta_box['id'] != "thesis_javascript_meta") {
          add_meta_box($meta_box['id'], $meta_box['title'], array('thesis_post_options', 'output_' . $meta_name . '_box'), 'guide', 'normal', 'high'); #wp
        }
      }
      add_action('save_post', array('thesis_post_options', 'save_meta')); #wp
    }
    
  9. Rich

    Smart man! I think that will work. I will give it a try and get back to you.

  10. Rich

    Dimas – You definitely pointed me in the right direction.

    The trouble is the [expected-value].
    During the save_meta() function call, the nonces are verified via:

    wp_verify_nonce($_POST[$meta_box['noncename'] . '_noncename'], plugin_basename(__FILE__))
    

    So, when creating the [expected-value], you need to use the same $action argument that the save_meta() function is using. Since my save_post action is called from custom_functions.php, and the save_meta() function is called from options_post.php, plugin_basename(__FILE__) will be different, and the nonce would fail.

    So, I did some ugliness (hard coded the path to the options_post.php file) to make it work for now, but here it is:

      function guide_admin_menu()
      {
        $post_options = new thesis_post_options;
        $post_options->meta_boxes();
        foreach ($post_options->meta_boxes as $meta_name => $meta_box) 
        {
          if ($meta_box['id'] != "thesis_multimedia_meta" && $meta_box['id'] != "thesis_javascript_meta") {
            add_meta_box($meta_box['id'], $meta_box['title'], array('thesis_post_options', 'output_' . $meta_name . '_box'), 'guide', 'normal', 'high'); #wp
          }
        }
       //add_action('save_post', array('thesis_post_options', 'save_meta')); #wp
      }
    	
      add_action('save_post', 'nonces_for_undisplayed', 5);
      function nonces_for_undisplayed()
      {
        $post_options = new thesis_post_options;
        $post_options->meta_boxes();
        foreach ($post_options->meta_boxes as $meta_name => $meta_box) 
        {
          if ($meta_box['id'] == "thesis_multimedia_meta" || $meta_box['id'] == "thesis_javascript_meta") {
            $_POST[$meta_box['noncename'] . '_noncename'] = wp_create_nonce('var/wp/wp-content/themes/thesis_18/lib/admin/options_post.php');
          }
        }
      }
    

    Note: during all of this, I realized that line 61 of your original post is not necessary. The action to save meta data during save_post had already been called. It isn’t necessary to call it again.

    I also removed line 48, adding the action to admin_menu. Instead, I call the function that adds the meta boxes via the ‘register_meta_box_cb’ argument of register_post_type, which is called during ‘init’

  11. Mark B

    I know this post is nearly a year old now but I have a question that i can’t seem to find an answer to.

    I have multiple custom post types and I wish to add the thesis seo and meta boxes to all of them but I get a repeat error when I do. The first is fine and works perfectly using the code above. Unfortunately I cannot find a way to duplicate this for my other custom post types. Can anyone shed some light on this?

    Thanks

  12. Idris

    great tutorial…thank you for sharing

  13. Kimberly Castleberry

    Great tutorial. Stopped by your blog after hyperarts linked to you and definitely will be back. I do a lot with thesis and so these kinda things come in handy.
    Kim
    PS: You asked in another post about spam control. I’ve had trouble with spamfree so I rely heavily on GASP (growmaps anti spambot plugin) which works well for stopping bot traffic.

  14. barefoot

    Thanks very much for this. The menu showed up fine. I reset the permalinks. Now, I’m curious, since I didn’t CPT up when I first started (it’s a recipe blog), and now I’ve added a new ‘recipes’ CPT; do I just go in and transfer the content from the regular post type in to the custom recipe post type? And then erase original post?

  15. Raymund

    Hi, do you have a tutorial on how to reverse the chronology of posts in the homepage from the oldest to the newest? Thanks.

  16. exedesign

    Hello,
    Great custom meta box helper code you got here. I have been checking out your tool, but I wonder do I need to separately create custom post type or is there a better way to do it within your tools?
    I have seen other that get you everything (post types and metaboxes in one place: http://wp.tutsplus.com/tutorials/creative-coding/custom-post-type-helper-class/)

    Thanks