How to Enable Wordpress to Define the Parent of a Custom Post Type

Beacon Blog Article

By Beacon News | Published October 12, 2012 | Categories: Web Development

Here is a script that will enable you to define a parent for a custom post type. You just need to include this in your functions.php file. In this code sample, “Product” is my custom post type but you will need to replace this with whatever you are wanting to define a parent for.

[php] //Add the meta box callback function function admin_init(){ add_meta_box(“Product_parent_id”, “Product Parent ID”, “set_Product_parent_id”, “Product”, “normal”, “low”); } add_action(“admin_init”, “admin_init”);

//Meta box for setting the parent ID function set_Product_parent_id() { global $post; $custom = get_post_custom($post->ID); $parent_id = $custom[‘parent_id’][0]; ?>

Please specify the ID of the page or post to be a parent to this Product.

Leave blank for no heirarchy.  Products will appear from the server root with no assocaited parent page or post.

<p>&amp;#160;</p>
<p>[php]<br/>//Add the meta box callback function<br/>function admin_init(){<br/>add_meta_box("Product_parent_id", "Product Parent ID", "set_Product_parent_id", "Product", "normal", "low");<br/>}<br/>add_action("admin_init", "admin_init");</p>
<p>//Meta box for setting the parent ID<br/>function set_Product_parent_id() {<br/>global $post;<br/>$custom = get_post_custom($post-&gt;ID);<br/>$parent_id = $custom['parent_id'][0];<br/>?&gt;</p>
<p>Please specify the ID of the page or post to be a parent to this Product.</p>
<p>Leave blank for no heirarchy.&amp;#160; Products will appear from the server root with no assocaited parent page or post.</p>
<p><input id="parent_id" name="parent_id" type="text" value="&lt;?php echo $post-&gt;post_parent; ?&gt;"/><br/><!--?php &lt;br ?--> // create a custom nonce for submit verification later<br/>echo '<input name="parent_id_noncename" type="hidden" value="' . wp_create_nonce(__FILE__) . '"/>';<br/>}</p>
<p>// Save the meta data<br/>function save_Product_parent_id($post_id) {<br/>global $post;</p>
<p>// make sure data came from our meta box<br/>if (!wp_verify_nonce($_POST['parent_id_noncename'],__FILE__)) return $post_id;<br/>if(isset($_POST['parent_id']) &amp;&amp; ($_POST['post_type'] == "Product")) {<br/>$data = $_POST['parent_id'];<br/>update_post_meta($post_id, 'parent_id', $data);<br/>}<br/>}<br/>add_action("save_post", "save_Product_parent_id");<br/><span style="font-family: monospace;">[/php]</span></p>

The 3 functions in this script are:
function admin_init() Which calls the meta box.
function set_case_study_parent_id() Which creates the metabox.
function save_case_study_parent_id() Which saves the meta data in the database.

Once this script is in place, when you got to one of your custom posts there will be a box at the very bottom that says "Parent Product ID". You must define the parent in this box by it's page ID and not the permalink name.

Also if you want the parent to show up in the URL, you must make sure that you include this line in the $args array when you register your custom post type in the functions.php file

 

<p>[php]'rewrite' =&gt; array('slug' =&gt; 'product', 'with_front' =&gt; true)[/php]</p>

You may have to go into your Permalinks settings and just click Save Changes to update the new URL structure.

Let's get to work!

Contact Us