Seeding wordpress custom post with sample data - php

I am building a wordpress site for a client. The concept they want to use is having a restaurant-menu be used as a boilerplate, but making fields like price and/or food items adjustable on a per location basis.
The approach I would like to use is:
Use custom post types
Use advanced custom fields
Pre populate the custom post type with sample data that will automatically be reproduced every time I create a new post
With 3. I am hitting a roadblock. I am looking for either a simple , plugin based way to achieve this or a PHP driven db insertion function. Doesn't matter to me, whatever works. Any resources would help!
Thanks
On this post the approach suggests using the wp_insert_post_data function
if (isAdmin()){
add_filter('wp_insert_post_data','custom_insert_post', '99', 2);
function custom_insert_post($data, $postarr){ <--
$data['post_title'] = 'default_post_title';
$data['post_status'] = 'future';
//.... etc
}}
I worry about the limitations of hard coding the database insertion if the ACF keys are changed in a future iteration, affecting the seeding method I've created initially. So I'm wondering if there is a plugin that allows me to pre-populate a given post type (id, key, ...etc), or a post in stack overflow that has solved a similar problem. Ideally, I'd like to have a given post generated in a GUI and then have that first insertion prepopulated in subsequent posts.
Desired output in wordpress back end

Related

ACF's Register Fields via PHP is DB and resource heavy

At my company we've implemented a system that programatically runs register_fields for each item in an array. These fields are then added to a group with the location of a Gutenberg Block. This works rather well with a fast DB connection but drags with a slower one.
I've found that the register_fields function actually is relatively resource heavy and does quite a bit of database work. I would rather not run this function on the frontend as we don't actually need the field data; just it's value?
Based on my understanding of Post Meta and Gutenberg Blocks the values we want should be fetchable without registering fields on the frontend as it's stored in the Post Meta? I've tried changing the field registration to admin_init instead but that caused return_format and various other properties to stop working. Is there any way to make this work so that I either don't have to register the fields or the registering is less heavy?

How can I retrieve values of Custom Field Suite plugin nested loop one by one?

I used Custom Field Suite to make a custom field using it's loop function and cannot get around retrieving the values one by one. The structure of the custom field is somewhat like this:
I'm just an amateur with regards to php so I haven't tried anything but scour the web for possible answer, but CFS has so little documentation on the web.
https://gyazo.com/6929bd0063d8a6ad0bd3881a368579d7
I would want to retrieve each post content on different parts of my php file. For example;post-content0 on top of the page and post-content5 on the last part of the page.

Wordpress Custom Table or Custom Post Types?

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,

Creating WP custom post types with custom fields from database

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.

Wordpress Plugin - Saving simple data about a post

I'm trying to create a plugin in wordpress that allows you to age restrict certain posts and pages but not others. I looked for a plugin that does this and couldn't find one. I've got all the coding covered except I can't figure out how to instruct wordpress to save the state of the checkbox as data associated with that post or page. I dissected a couple of plugins that have similar functionality but am mystified by how their code works and it breaks when I try to modify it for my use.
Can someone point me to a tutorial that explains how data gets saved from the post edit page and how to access that data later?
check out post meta-data :
http://codex.wordpress.org/Custom_Fields
The basic idea is WP allows you to add additional fields to a post, and then retrieve it during the loop. Just use this field to test against at render-time and display alternate content, should (in this case) your post be restricted.
The codex is pretty thorough on the subject, and I'm sure google can fill in the gaps -
Hope that helps

Categories