Meta_key and meta_value - Wordpress - 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

Related

Wordpress, get_posts does not returning all items with post_type=attachment

I've some broken media files in wordpress database and need to wp_update_attachment_metadata. I like to call all id for post_type "attachment". I using the script below. I do not get all entries only around 1000. But if I query in MySql i get something arround 4000. My script should provide everything also listed in WP Media. I've tested the array and some media entries are missing. Where is my misunderstanding?
Thanks!!
$attachments = get_posts(array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'numberposts' => -1,
));
return count($attachments);
My SQL Query to compare the data are:
SELECT * FROM `wp_posts` WHERE `post_type` LIKE 'attachment'
I've found the reason for all the missing id's.
First: My issue was generated by the WPML plugin. In translating a post the plugin duplicates its media. (SVG's with empty attachment_metadata are duplicated and the issue was crowing.)
Solution: My "get_post call" contains only the post of the current language. After deactivating WPML I've had exactly the same counts like in my MySql query. So I was finally able to use wp_update_attachment_metadata for all corrupted images.

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/

Create custom column in post table in 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

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

order by custom field include even if empty - wordpress

I have a query for a page of posts.
It return results based on a custom post type, and custom field value. Now I've added the ability to order the results based on another custom field.
$loop = new WP_Query( array ( 'post_type' => 'new', 'orderby' => 'meta_value_num', 'meta_key' => 'over-length', 'meta_query' => array( array( 'key' => 'over-make', 'value' => 'Doral', 'compare' => 'LIKE') ) ) );
I've run into a bit of a problem. I'm ordering the results by a custom field called 'over-length' but it seems that if a post doesn't contain a value for 'over-length' it is excluded from the results.
I'm wondering how could I change my code so that it included post that don't have a value for orderby.
Also just thought of a workaround, but not sure how to do it. I'm using a plugin called "more fields" to create my custom fields. Would it be easier to check if the 'over-length' field is empty and set it to 0? if so how do I go about doing this.
Update
I've looked into the issue a bit further. It seems that if no value is given to 'over-length' the custom field is not added to wp_postmeta in the database. If I give a post a over-length value then go back and remove it it does in fact include the result in my query as the field still exist in the database. So how can I get this custom field to be entered into the database if it has a value or not?
About your last request - for a way to add a meta_key (custom field) on all posts even if it is left blank:
I often use the plugin Custom Fields Template for this kind of thing. It is similar to the one you are using (more fields) but it gives you many possibilities to play with the fields, and most importantly for this case, you can adjust hidden fields and default values. You can perhaps set a default=' ' (space) to have the field inserted if left blank. Or, with a bit more work, modify the plugin code to insert all the fields to DB without checking for empty the values. In the case of CustomFieldsTemplate it shouldn't be hard to do it.

Resources