Create custom column in post table in wordpress - wordpress

I want to add two custom field to table wp_posts, and that I need for my plugin.
now to enable these fields I have changed core file of wordpress
wordpress/wp-admin/post.php
$data = compact( array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' ) );
here I have added two fields which I want.
Now I want these things as installable( I have manually added two fields here).
So how to do that within plugin.
I have read one post
http://wp.tutsplus.com/tutorials/creative-coding/add-a-custom-column-in-posts-and-custom-post-types-admin-screen/
here hooks are used in theme function.php,but i want to do in plugin itself.
I am using wordpress 3.6.
still any confusion please comment I will update.

As pointed in the question comments, you should never edit wp core files(reason: they get overwritten at updates), and you should never modify wp tables (can cause crashes at updates).
You are developing a plugin, then you have a few options for the database setup:
1) you can use existing database tables
* you might use the postmeta table
2) if for any reason you can't use post meta, create your own table
* add the 2 columns that you need and a column as post id, this way things will run smoothly
PS: you can use in your plugin all wordpress functions, just search them in the codex and see how you need to use them.
also check this info about creating database tables with plugins

Related

How to create a custom meta table in wordpress?

I have custom meta box with multiple fields and it is working fine. Now, I want to store this meta box data into a custom table. So how can I do that ?
I have researched on google and Youtube but didn't got what exactly I am looking for.
If anyone can provide me with Step by Step guide or tutorial links then it will be very much helpful.
I'm not entirely sure why you would want to store Post Meta into a separate table, but I've had situations where I've needed to do crazier things.
The gist would be that you can use your Custom Meta Box to display the form fields, and then handle those form fields with your own custom function on the save_post hook.
Let's say you've registered a custom meta box with <input name="my_custom_table_field" /> in it. Instead of using update_post_meta() on the save_post hooks, you could write a function that manages the data with the $wpdb->update method. Something like this would get you started:
add_action( 'save_post', 'save_my_custom_data' );
function save_my_custom_data( $post_id ){
global $wpdb;
if( isset( $_POST['my_custom_table_field'] ){
$result = $wpdb->update(
'my_custom_table',
array(
'post_id' => $post_id,
'my_custom_table_field' => $_POST['my_custom_table_field'],
),
array(
'post_id' => $post_id
),
array(
'%d',
'%s'
),
array (
'%d'
)
);
});
}
You'll want to make sure to handle the data appropriately before saving it, of course. And again, I'm not sure why you'd need a custom "meta" table, but farbeit from me to say you "shouldn't" (especially depending on your particular usecase) - but something like the above would get you started.
To summarize:
Display your custom meta box with WP's metabox functions
Handle the save_post hook for your custom fields separately
Sanitize, trim, or otherwise make sure the data being stored is supposed to be stored in accordance to best practices for the field type
Make use of the global $wpdb class to update it.
Also of note, this answer doesn't go into CREATING the database table - because that depends on your storage engine, particular indexing needs, etc. But in general you should be able to search for "create database table in [whatever storage language]" to get a walkthrough of creating the table - then use the outline above to store the data in it.
This is the official Wordpress guide: https://developer.wordpress.org/plugins/metadata/custom-meta-boxes/

Meta_key and meta_value - Wordpress

As the question asks, I'm trying to understand one thing, as this is a popular of the fields within the table, but I can't understand their usefulness.
global $wpdb;
$wpdb->insert(
$wpdb->postmeta,
array(
'post_id' => '1',
'meta_key' => 'address',
'meta_value' => '1428 Elm St.'
),
array(
'%d',
'%s',
'%s'
)
);
One thing is the post-table table?
What is the use of this table as in the previous case, for the seo?
in my searches on google, I saw that setting the correct tags, involves a better indexing by google, and for this there is a truly spectacular tool, which allows you to have access by entering the word of the topic we want to write like post and get all the queries, that people type more on google: (https://answerthepublic.com/)
but setting tags and populating the postmeta table are not the same thing?
What is the utility of populating with a code like the postmeta table before?
Its NOT for SEO. Its for custom fields. On WP by default for posts and pages you have wp-post table, that tables has following columns (https://i.imgur.com/NCBslgq.png) for expanding functionality and adding extra custom fields WP has other table wp-postmeta (meta_id, post_id, meta_key, meta_value). So if you want add address field to post or custom post, you attaching meta field to that post using add_post_meta() https://codex.wordpress.org/Function_Reference/add_post_meta

Change name of custom post type after using it

I want to change the name of a custom post type after I'm using it for a long time.
The current name is registered like this:
register_post_type( 'toremove-name' , $args );
And I want it this way:
register_post_type( 'name' , $args );
If I change it, I think it will make problems in the DB.
Is there a way or a plugin to solve this?
I'm sure there is a plugin, but if you have MySQL access, you can also just rename the CPT the way you have indicated and perform am SQL query to update:
UPDATE wp_posts SET post_type = New CPT Name WHERE post_type = Old CPT Name;
Also, don't forget your taxonomies if you have any registered to the CPT. You might have to flush your permalinks after you performed this action

SQL Server to CSV to Wordpress - is it possible?

I have a custom made CMS system with a relatively small number (~1000) of articles inside my SQL Server database.
I want to migrate the whole site to Wordpress. Is it possible and if so, then how, to migrate the data (one table - Article) from SQL Server to a CSV file and then import it into Wordpress?
Thanks!
update:
The structure of table Article looks like this:
ID_Article - int (pk)
Title - nvarchar(max)
Summary - nvarchar(max)
Contents - nvarchar(max)
Date - datetime
ID_Author - int (fk)
Image - nvarchar(max)
Promoted - bit
Hidden - bit
There also categories done with an associative CategoryToArticle table. The only thing that needs to be moved is CONTENTS (optionally merged with Summary) DATE and TITLE (would be cool if it was merged with author's name "John Doe: My Article title."). It can be categorised as "Archive" or soemthing like that, image and other flags can be dropped completely.
You could use an automation software such as BlogSenseWP to import the CSV items in as new posts, but you would have limited control over the dating and tagging would have to be auto-regenerated using the Yahoo Tags API (included in the software's features). Categorization might be tricky as well, but there are keyword based auto-categorization filters that could help you get close.
Otherwise you would might want to investigate a custom PHP script that imports the CSV fields desired into variables, and then loops through them adding them to the wordpress database using wordpress functions.
Here is a custom way of adding a post to wordpress:
$permalink_name = sanitize_title_with_dashes( $title );
$post = array(
'post_author' => $author_id
'post_category' => $cat_id,
'post_content' => $description,
'post_date' => $date_placeholder,
'post_date_gmt' => $gmt_date,
'post_name' => $permalink_name,
'post_status' => 'publish',
'post_title' => $title,
'post_type' => 'post',
'tags_input' => "$tags",
'original_source'=> $link
);
$post_id = wp_insert_post( $post, $wp_error );
You would have to include the wp-config.php file at the top of this script to load the wordpress code environment.
The above is a crude summary. It would take a thorough understanding of PHP to fill in the ommited code and complete the concept script.
Use the SQLCMD command line tool. You can look at this question to see how to export data into a CSV.
It is possible. However, it heavily depends on the structure of your current table. If you have tags, categories, different post statuses (published, pending etc).
One possible way would be to write a script to read from your database, and drop the same data into the wordpress tables.
MSSQL -> CSV -> MySQL is also possible. You'll just have to read the CSV and dump the data into MySQL.
Maybe if you can give your table structure, we can give you a better way.
this might help you
first create a table in mysql DB
CREATE TABLE Genesis (
id INT(10),
title varchar(255),
description text,
date timestamp,
PRIMARY KEY (id)
);
then use some php code to updae wordpress data.
source -
http://web-design101.com/createawebsite/featured-articles/insert-wordpress-posts-through-mysql

migrating node references

I am working on a project to migrate a website from asp.net to drupal architecture. But the site content is very hierarchal and has a lot of references between entities.
for example: each content belongs to a category, and each category belongs to another category section. Now there may be another level of hierarchy even.
I am planning to make use of migrate module for migrating the database content and linking the migrated nodes via a node reference field.
But i am getting stuck with migrate module as i can't find a way to migrate the node reference field anywhere...
can anyone help me out with this...
Actually, it doesnt seem to be that hard .. in 2012. Yes, you have to keep track of source IDs versus import IDs, but the migrate module does this for you, in a neat little table. You can join that table in your source query, and update the node reference field with the nid of the .. referenced node. Ofcourse, the referenced nodes should have already been imported. If they werent, you can run an run an 'update' later and referenced nids get entered based on the latter imports too. In practice:
$query = Database::getConnection(
'default', 'mysourcedb'
)->select(
'mysourcetable','source'
)->fields('source', array(
'id',
'title',
'whatever'
'rel_rec_id'
)
);
$query->leftJoin('migrate_map_relimport','relmap','relmap.sourceid1=source.rel_rec_id');
$query->addField('relmap','destid1','rel_node_id');
The code above assumes you have a 'mysourcedb' with a 'mysourcetable' in it that refers to a 'rel_rec_id', and theres another import called RelImport that imports the rel table that rel_rec_id is refering to; it should have already run (or will run before you run an additional update). Do a migrate-status once you have the RelImport class to make sure the table exists.
To be able to make joins to the 'migrate_map_relimport' table, make sure map tables are written to the source database, not the drupal database. This is not always necessary, but here it is:
$this->map = new MigrateSQLMap(
$this->machineName,
array(
'id' => array(
'type' => 'int',
'unsigned' => true,
'not null' => true,
'alias' => 'source'
)
),
MigrateDestinationNode::getKeySchema(),
'mysourcedb' // connection to use to write map tables
);
and finally, assign the retrieved rel_node_id to your node reference:
$this->addFieldMapping( 'field_rel_node', 'rel_node_id' );
Yay, it is rocket science .. YMMV
As far as I know, you will not be able to do this entirely within the migrate module. You'll have to run a few queries directly in MySQL.
Essentially, you'll have to create an extra field in each content type to house their legacy ID's and an additional field for each legacy reference (in addition to the actual nodereference field). Load all the data into the content types (leave the nodereference field empty). Then once all of the entities are loaded in, you run mysql queries to populate the nodereference fields based on the legacy IDs and legacy reference fields. Once that's done you can safely delete these legacy fields.
Not the most elegant solution, but I've used it many times.
Caveats:
This is for Drupal 6; Drupal 7's fields implementation is totally different AFAIK.
For very large migrations, you might want to do your legacy field deletions through MySQL.
You might also take a look at the code for the Content Migrate module (more information at https://drupal.org/node/1144136). It's for migrating D6 Content Construction Kit (CCK) content to D7 Fields and it's been integrated with the References module.
It won't do what you need out of the box, as you're coming from an ASP.net site instead of a D6 site, but it may provide some clues.

Resources