Indispensable MySQL queries for custom fields in WordPress


stores the data (articles, pages, categories, etc) in a MySQL database. It is therefore useful to know some codes to create a custom based website.

 

Note: This post includes MySQL queries for creating the custom fields in WordPress CMS (you can use for based design too). Before working in your database, take a backup first. You are responsible for your actions!

 

Disclaimers

 

Using phpMyAdmin

 

To the applications listed here you should be able to log in to phpMyAdmin, a tool generally accessible from the administration of your host (via cPanel or Plesk for example).

Then go to the tab corresponding to the SQL database previously selected in the left side of the

If you have never used phpMyAdmin, be careful!

 

Prefix Base wp_

 

For each of the queries below, remember to replace the prefix wp_ by one for your MySQL database.

It is highly likely that you do not keep the default prefix as recommended for safety reasons.

 

Backup your database

 

Before working in phpMyAdmin, backup your MySQL database.

 

Queries for custom fields

 

Add a custom field to all articles and pages

 

INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'OwnCustomField'
AS meta_key 'myvalue AS meta_value FROM wp_posts
WHERE ID NOT IN (SELECT post_id FROM wp_postmeta WHERE meta_key = 'MonCustomField');

 


This adds a custom field (custom field) to all items in the database.

Consider replacing OwnCustomField by the name of the field in question and MyValue by the value of your choice.

 

Add a custom field in articles only

 

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'MyCustomField'
AS meta_key 'myvalue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'MonCustomField')
`` AND post_type = 'post';

 

Same as above – with the same advice, but only for the articles.

 

Add a custom field in pages only

 

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT ID AS post_id, 'MyCustomField'
AS meta_key 'myvalue AS meta_value
FROM wp_posts WHERE ID NOT IN
(SELECT post_id FROM wp_postmeta WHERE meta_key = 'MonCustomField')
AND `post_type` = 'page';

 

Same as above – with the same advice, but only for pages.

 

Signature


0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave Lame Behind - Get Engaged

Here’s what we’ve got for you:

Contact Us Today for Smarter Marketing of Knowledge

See each of the premium resources below:

For Publishers and Content Owners

Do you want to optimize your available content?

Do you want to increase the visibility of your content?

Do you want to increase your reliability as an Author?

Do you want your next or existing project to be technically planned?

If your answer to any of these is YES contact us with your approximate budget.

Contact Us Today

For the Media Companies and Advertisers

You must be seeking a publisher website like ours with real content to give an exposure to your client's business?

We provide Industry's Standard opportunities for advertisement.

Do you want some promotion that comes with proper invoicing, SLA on per annum basis?

If your answer is YES contact us today for a quote.

Contact Us Today for Quote

Mentions

  1. 20 Helpful Wordpress SQL Queries Snippets says:

    [...] SourceDelete Post MetaWhen you install or remove plugins they make use of the post meta to store data. [...]

  2. 20 Helpful WordPress SQL Queries Snippets- rampantdesigntools.com says:

    [...] Source [...]

  3. 20 Helpful WordPress SQL Queries Snippets | The best Tutorials says:

    [...] Source [...]

  4. 20 Helpful WordPress SQL Queries Snippets | Wbcom Designs says:

    [...] Source [...]

  5. 20 Helpful WordPress SQL Queries Snippets | Typography Blog says:

    [...] Source [...]

  6. 20 Helpful Wordpress SQL Queries Snippets | JC says:

    [...] Source [...]

  7. 20 Helpful WordPress SQL Queries Snippets | Programming Blog says:

    [...] Source [...]

  8. 20 Helpful WordPress SQL Queries Snippets « Free UK Web Design says:

    [...] Source [...]

  9. Wpfail.com » 20 Helpful WordPress SQL Queries Snippets says:

    [...] Source [...]

  10. 35 Useful WordPress Tutorials From 2011 - WP Mods says:

    [...] Indispensable MySQL queries for custom fields in WordPress (The Customize Windows) [...]

  11. wpengineer.ru/2011/35-poleznyih-sovetov-2011-goda says:

    [...] ???????? MySQL ??????? ??? ?????? ? ????????????? ?????? [...]

  12. 50 Unique and Informative wordpress tutorial showcase - tripwire magazine says:

    [...] 24. Indispensable MySQL queries for custom fields in WordPress [...]

  13. » 50 Unique and Informative wordpress tutorial showcase stuff i like says:

    [...] 24. Indispensable MySQL queries for custom fields in WordPress [...]

  14. 50 Unique and Informative wordpress tutorial showcase - WordPress Vampire says:

    [...] 24. Indispensable MySQL queries for custom fields in WordPress [...]

  15. 50 Unique and Informative wordpress tutorial showcase – wordpress multiple blogs says:

    [...] 24. Indispensable MySQL queries for custom fields in WordPress [...]

  16. [...] 24. Indispensable MySQL queries for tradition fields in WordPress [...]

  17. [...] we wrote Indispensable MySQL queries for custom fields in WordPress, this time few examples of SQL [...]

  18. 20 Helpful WordPress SQL Queries Snippets says:

    [...] Source [...]

  19. 33 Useful SQL Queries For WordPress | WooShift says:

    [...] Source [...]

  20. WordPress tip: Add a custom field to all posts says:

    [...] to Abhishek for the tip! If you enjoyed this article, please consider sharing it! Tweet [...]

  21. WordPress tip: Add a custom field to all posts says:

    [...] to Abhishek for the [...]

  22. Aggiungere campi personalizzati a post e pagine in WordPress says:

    [...] campi personalizzati a post e pagine in WordPress Abhishek ha pubblicato tempo fa un interessante articolo dedicato [...]

  23. How to Add a Custom Field says:

    [...] Thanks to Abhishek for the tip! This entry was posted in WordPress Tips and tagged custom field, phpmyadmin, posts, [...]

  24. How to Add a Custom Field in WordPress to all Posts! says:

    [...] WHERE meta_key [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>