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.
Related
I am working on a wordpress website and I am using Advanced Custom Fields and Custom post type UI plugins. What I want to do is that:
The website I am working on was in native php and I want to migrate from native to wordpress but I face this problem, I can't achieve the relations that I had in mysql, in the form of post type and custom field groups in wordpress and ACF.
SO any have an Idea how to do this.
I also checked reusable fields but It actually won't help
https://github.com/tybruffy/ACF-Reusable-Field-Group
You didn't post your Data Model so it's hard to tell what the issues are or to give you an example based on it.
However, regarding the way Wordpress handles custom post type and meta value, I suggest that you consider post types as entities while meta values (custom fields here as you used ACF) usually their properties.
Usually, modelling 1-M or M-M relationships can be done in Wordpress by introducing custom taxonomies normally solve the problem, since it works as a mechanism to classify, to categorize and to group your entities. An example of this is the way Woocommerce give options to treat their product attributes as custom taxonomies. You can view the code from the link below.
https://docs.woocommerce.com/wc-apidocs/source-class-WC_Product.html#1398-1417
This is perhaps a very basic Wordpress development question. I am aware about creating custom types in Wordpress. So I created a Product type and a Version type and I would like to be able to keep a one-to-many relationship between the two i.e. for every Product, I would like to be able to associate one or more Version items from the admin editor. What is the way to do this? I am not in favor of using plugins, so I was thinking, I would use some kind of a custom field array of IDs, to connect the two. Am I on the right way? If I have to use a plugin, which one should it be?
The easy way to do this would be to add a meta box on your product admin page. That way you can create a drop down list on your product page with your versions. You can get more on adding meta boxes using add_meta_box here.
You add all required functions to your themes' functions.php, but to summarise you will need the following wp functions amongst others.
add_action
add_meta_box
update_post_meta
Looking up these functions in the Wordpress Codex (using google) will get you on your way. If you get stuck, post some code of what you have tried and take it from there. There are a number of tutorials/examples around. If you can do enough to create custom types this should not take you long.
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 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.