Title: [Plugin: Custom Upload Dir] Hack/desired feature: %post_parent%
Last modified: August 20, 2016

---

# [Plugin: Custom Upload Dir] Hack/desired feature: %post_parent%

 *  Resolved [frizzle](https://wordpress.org/support/users/frizzle/)
 * (@frizzle)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/plugin-custom-upload-dir-hackdesired-feature-post_parent/)
 * I’ve been using CUD for a site where it would be really useful to have images
   sorted by page parent. So I made it so that CUD could accommodate this very easily,
   including with posts that have multiple parents.
 * It’s pretty easy. In custom_upload_dir.php, after “$want_name = (strpos($customdir,‘%
   post_name%’) !== false);”, add the following code:
 *     ```
       $want_parent = (strpos($customdir,'%post_parent%') !== false);
       	if($want_parent) {
       		if($post->post_parent) {
       			$post_parent_slug = basename(get_permalink($post->post_parent))."/";
       		} elseif (get_post_type_object($post->post_type)->rewrite["slug"]) {
       			$post_parent_slug = (get_post_type_object($post->post_type)->rewrite["slug"])."/";
       		} else {
       			$post_parent_slug = "";
       		}
       	}
       ```
   
 * And in the list of “$custom_dir =” calls, add these two:
 *     ```
       $customdir = ($want_parent)										?	str_replace('%post_parent%/', $post_parent_slug, $customdir) : $customdir;
       	$customdir = ($want_parent)										?	str_replace('%post_parent%', substr($post_parent_slug,0,strlen($post_parent_slug)-1), $customdir) : $customdir;
       ```
   
 * The funny multiple calls and so on because it tries to auto-detect whether backslashes
   already exist or not.
 * If the post in question is a custom post type, it uses the custom post type slug
   name instead of the parent name (because there will probably not be a parent).
 * Hope that helps someone… I release the above lines of code into the public domain,
   of course.
 * [http://wordpress.org/extend/plugins/custom-upload-dir/](http://wordpress.org/extend/plugins/custom-upload-dir/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/plugin-custom-upload-dir-hackdesired-feature-post_parent/#post-2257114)
 * Thanks. I’ve rewritten much of the plugin recently and am currently testing the
   changes before release. I’ll take a closer look on your addition next week and
   probably release very shortly.
 *  Plugin Author [ulfben](https://wordpress.org/support/users/ulfben/)
 * (@ulfben)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/plugin-custom-upload-dir-hackdesired-feature-post_parent/#post-2257170)
 * Sorry. Forgot about your thread. parent_slug is in the [development version](http://wordpress.org/extend/plugins/custom-upload-dir/download/).
   Please test it for me.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘[Plugin: Custom Upload Dir] Hack/desired feature: %post_parent%’ is closed
to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/custom-upload-dir.svg)
 * [Custom Upload Dir](https://wordpress.org/plugins/custom-upload-dir/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-upload-dir/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-upload-dir/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-upload-dir/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-upload-dir/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-upload-dir/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [ulfben](https://wordpress.org/support/users/ulfben/)
 * Last activity: [14 years, 5 months ago](https://wordpress.org/support/topic/plugin-custom-upload-dir-hackdesired-feature-post_parent/#post-2257170)
 * Status: resolved