How to use custom post type APIs, but use a different db table

We currently have a plugin that creates a new WP table, but it recreates the wheel when it comes to managing/updating data. It would be a good candidate for custom post types, but we don’t want to pollute the wp_posts table.

Use case: We’re creating a specific job board for a company that lists 100s of jobs, and we want to keep the tables clean for other php scripts outside of WP that will also be accessing the same data. Input and management would be handled by the standard WordPress APIs.

Question: I want to use WP’s custom post type functionality, but use a different db table. Thoughts? Directions? Is it possible to simply tell WordPress to switch tables for a certain query?

Related: Difficult to read and understand, and not entirely the same:
Use Custom Database with Custom Post Type

1 Answer
1

I don’t think you can do this. Maybe you could use a whole new database, but it seems unlikely you could add a new table for managing a custom post type. For one thing, a custom post type is stored in the wp_posts table, but some of the data for it gets stored in the wp_postmeta table (especially any post meta fields). And if you have a taxonomy with it, that uses 3 different tables. And if your plugin needs to store any options those would be in another table.

I wouldn’t worry about polluting the wp tables, it is how they are made to work. Part of its power is the extendability that is built in and easy to use.

You could maybe provide a way to remove your post type and all of its posts if you wanted to be able to completely remove it.

Leave a Comment