Skip Navigation

[Resolved] Create posts from existing files on server

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by larryL 5 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#1208031

Hi, I have a somewhat strange question.

I created a custom post type using Types called 'Recordings'. On the server I have a directory in /uploads that I'd like to use to generate posts from. That directory has lots of subfolders in it. For example:

./uploads/primary/secondary/Call Recording/file1.mp3
./uploads/primary/secondary/Call Recording/file2.mp3
./uploads/primary/third/Call Recording/file1.mp3
./uploads/primary/third/Call Recording/file2.mp3
./uploads/primary/third/Call Recording/file3.mp3

I would like to be able to create a post (in the Recordings custom post type) using the title of the file, and attach the file to it (probably using a custom field).

Is this something you could help me do?

Thanks!

Larry

#1208167

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Larry

Are you suggesting you want to scan the uploads directory and automatically generate posts from the files that are found there?

That would be a bit of a strange question.

You would need to write a custom script for that (which you would run one time only; helpfully the code snippets you can add at Toolset > Settings > Custom Code let you do so), and it's outside of our support policy to provide code in a case such as this.

I did a quick bit of Googling and think this forms the basis of what you need: hidden link

You would scan the uploads directory for mp3 files using the glob function which would return all the results as an array.

You would then loop over that array and create a new post (using wp_insert_post) which you update with the file URL as a custom field (using add_post_meta).

That sounds do-able.

If you are not the person to do it you would need to hire a developer (and WordPress developer, I don't think you need a Toolset developer specifically).

#1208688

Hi Nigel,

Let me ask this a different way, and thank you very much for finding the method!

I think I found a way to scan and import non wordpress inserted media items.

How would I use wp_insert_post (along with add_post_meta to add these posts (after the fact). Also, this would probably have to run on a cron since these files would be coming on a daily basis. So I guess the trigger would be IF a new file appears in X* directories?

Sorry if this out of scope just let me know....

Thanks,

Larry

#1209447

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Larry

It sounds like you would need to use a cron job to check this daily, see https://codex.wordpress.org/Function_Reference/wp_schedule_event (there are also a number of plugins you could use for this).

As for creating the posts themselves, well that means looping over the found files and using the standard WordPress function wp_insert_post to publish the post (https://developer.wordpress.org/reference/functions/wp_insert_post/), and once it was published add the custom fields using add_post_meta (https://developer.wordpress.org/reference/functions/add_post_meta/).

Sorry I can't really give you a tutorial on how to do that. The one thing you should be aware of is that, if you register the post type and custom field with Types, then the custom field meta key (as stored in the db) is the field slug with a 'wpcf-' prefix, e.g. a custom field "audio-file" would be stored with a key wpcf-audio-file.

#1218366

My issue is resolved now. Thank you!