I have a custom page on my WP instance which I want to interact with my database instance, ideally the standard table WP uses for posts.
This page pages retrieves an online stream from a set of social network channels, and I do want to persist this stream into the database so to form a sort of "archive".
So I have two questions:
a) Would it better creating a separate table, or is it more convenient adding everything in the wp-post table (or whatever it is called), classifying the item as social or something else
b) How can I interact with my WP database using WP Api so to not reinvent the wheel?
Thanks
Normally, we should only create our own tables if WordPress features and organisation are not enough to deal with our data.
Nowadays, Custom Post Types (wp_posts db table) and Custom Fields (wp_postmeta db table) are used in a variety of plugins (from Advanced Custom Fields to WooCommerce).
Then we use, among others, WP_Query to retrieve data and wp_update_post() and update_post_meta() to insert data.
In any case, data validation and security checks are always important.
Related
My client wants to have a Wordpress site with two separate types of users in mind. For each of these personas, different post content will be displayed to the user.
Currently, I'm using Query Wrangler to separate content. However, as far as I can tell, it does not have the ability to default to one persona and keep it throughout the site. It's essentially two separate sites I'm building, but with some shared content, which is what's making this difficult.
Are there any plugins or workarounds I can use to get the functionality I need?
I use custom templates along with this plugin to control what types of content my WP users see . http://themehybrid.com/plugins/members It allows you to create custom roles and check against those roles at anytime.
I'm making a plugin for WordPress and it requires some custom tables of mine (or the data in them, to be specific). These tables are simple. They only contain an id and a varchar field. Do not mention meta fields. I am using custom tables for a reason. The problem is, I need to allow the user to edit the data in these tables. To insert, update and delete rows in the tables. How do I handle this in WordPress?
I can make my own custom pages and create a whole system for editing, inserting and updating them but that's a workaround since none of that would be native to WordPress.
The only thing I came up with is making them into custom post types and having WordPress let me use its native adding, editing and deleting menu (the same one used in posts, pages, etc.). Though then new problems would arise. I need to connect the post ids to the rows in the custom tables and be very careful when editing, updating and deleting to make sure that both the posts and my custom table data remain consistent with each other. And that's a whole new layer of complexity.
So I was wondering if there's an elegant way to handle these sorts of simple tables. If there isn't I'll find my own workaround.
EDIT:
The plugin I'm making is supposed to turn your WordPress site into a kind of an ad posting site. And I need the custom tables to provide details about the ads.
refer this codex Wp_list_table. you can play with custom table data. for more you can go through an example plugin.
I am using wordpress to make a site which has job posts, and I want to know the best way to go about storing company details, as each job post must be linked to a company.
Normally I would have just set up a companies table in my database, and put a company ID within each job post, then joined to get company details.
However I have often come across the idea that it's not always the best option in Wordpress development to create new tables (such as here). Instead I would be better using custom post types.
But how would this work to store company details? Would it be right to create a custom post type for companies, and then add a new post for each company? And then use custom fields to store data about each?
Or would I be best off creating new database tables? What is better and why?
I suppose it is the best to use custom post types with post meta in conjuction with meta boxes. This way, you will be able to manage in ease your companies, and yet, you will be able to operate custom queries on companies via the WP_Query class.
Currently I am building a business directory for a my client, so I decide to use the custom post types because of flexibility. Also I made a custom script for managing company information in conjuction with post meta.
Take a look in the screen shot to undestraint what I mean :
Note that whenever I like to add new data to companies, I am free to just add some extra meta boxes, and then save the extra data as meta data for the post. This way is matter of minutes to extend a standart data entity.
What you need:
http://codex.wordpress.org/Post_Types
http://codex.wordpress.org/Function_Reference/add_meta_box
http://codex.wordpress.org/Function_Reference/register_taxonomy (optionaly)
http://codex.wordpress.org/Function_Reference/get_post_meta
http://codex.wordpress.org/Function_Reference/add_post_meta
I hope you this will help you :)
Create an Custom post type
add the custom fields to that custom post type for different fields you need,
There is no need of creating table manually,
http://wordpress.org/plugins/custom-post-type-ui/
http://wordpress.org/plugins/advanced-custom-fields/
and you can apply the sorting on some custom field you added to the post type,
I'm using the advanced custom fields plugin to create relationships between custom post types. My custom post types are course and institution. So when I add a course post via the admin dashboard, I select an institution related to the course.
However now I am inserting posts programmatically via a wpdb object, and I want to create the same relationships. Therefore I need to know how the ACF plugin is creating relationships.
At the moment I think I should just insert extra data as post-meta for the course. But is this all that is needed? Do I not need to also update the institution to have the course?
It's better to use WP and the plugin's API for that. Doing it with $wpdb is not a good idea, because other plugins may hook into wp_insert_post and if we use $wpdb it completely bypasses that. If we study the function, we can see that there's a lot going on there, not only a wpdb->insert.
ACF has its own logic to store meta data, simply using WP's update_post_meta() is not enough.
After doing a wp_insert_post(), use the returned ID to do an update_field().
My point is: we don't need to know "How are WordPress Post Relationships Created in the Database?". We use the functions provided by the platform and we have a code that's future proof.
I'm working on a Wordpress website that uses an external system to sell tickets. This ticket system provides a WordPress plugin that automatically creates and updates two new WordPress database tables: events and shows. That's great, but that's all this plugin does.
I have used the Database Browser plugin to test if the database tables created by the ticket system plugin were there and that was the case. I can see all the data there too. So far so good.
I now need to create custom post types (events and shows) with custom fields using the data from these database tables. I've searched, but I can't seem to find out how to create custom post types that pull their data from existing database tables.
Maybe I'm looking at this all wrong. Any help to point me in the right direction?
To create custom post refer this http://codex.wordpress.org/Post_Types#Custom_Post_Types
And use this plugin to create custom field http://wordpress.org/plugins/advanced-custom-fields/
i'm assuming you realise that you'll need to write code to query db...there are no plugins to query a db (that i know of).
it also sounds like you are going to duplicate data ie pull data from custom table events and shows, then add to this data to custom fields. i'm not sure this is the best approach.
also, if data in these custom tables change, will it be reflected in your custom fields data?
have i assumed too much? let me know.
To query db, use the wp class wpdb. I havent really used it but it has enough features to do what you want.
acf plugin has already been mentioned. custom post type ui plugin is easy for creating custom post types with no code. it even prints out the actual code for you so you can copy it, place in functions.php, then disable plugin, nice.