Searching with "Next Post ID" is giving me the Previous/Next Post Navigation, but my question is not related to that.
In WordPress Admin panel, when we're attempting to create a new post the post is been automatically saved as 'auto-draft' and the post ID is been placed within the form as a hidden field named post_id
. So when the post is submitted and the $_REQUEST['post_id']
is been isset.
I'm trying to move uploaded attachments with a Custom Post Type to it's designated directory under uploads/
like uploads/my-dir/my-item-{post_id}
. I'm following Rian Rietveld's snippet for the move. And attachments from admin panel is moving perfectly:
/**
* Set the attachment Upload path.
*
* Set a custom upload path for the post attachments for
* easy management.
*
* @param array $upload Default directory.
*
* @author Rian Rietveld
* @link
*
* @return array Defined directory for the App.
*/
public function my_attachment_dir( $upload ) {
if ( ! empty( $_REQUEST['post_id'] ) ) {
$id = $_REQUEST['post_id'];
// Check whether the posttype is ours.
if ( 'my-cpt' === get_post_type( $id ) ) {
$cpt_directory = "my-item-{$id}";
$upload['subdir'] = "/my-dir/{$cpt_directory}";
}
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];
}
return $upload;
}
add_filter( 'upload_dir', 'my_attachment_dir' );
But the issue is, I can't pass a hidden field with post ID from the front end, so the $_REQUEST['post_id']
is getting unset, and the filter is not working. The attachments are uploading to its typical year-month directory.
The question I have, could be answered by any of the following:
- How can I get the upcoming post ID in front end? or,
- How to add filter to
upload_dir
with a makeshift$post_id
instead of passing through$_REQUEST
?