• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » Indispensable MySQL queries for custom fields in WordPress

By Abhishek Ghosh March 12, 2011 8:23 am Updated on March 12, 2011

Indispensable MySQL queries for custom fields in WordPress

Advertisement

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

 

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

 

Disclaimers

 

Using phpMyAdmin

 

To run 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 interface.

Advertisement

---

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

 

Vim
1
2
3
4
INSERT 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

 

Vim
1
2
3
4
5
6
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 blog articles (posts).

 

Add a custom field in pages only

 

Vim
1
2
3
4
5
6
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
Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Indispensable MySQL queries for custom fields in WordPress

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • SQL Commands to Clean WordPress Database : Guru Killer Guide

    Here is a Guru Killer Guide With the Excellent SQL Commands to Clean WordPress Database Which can Reduce the Database Size Up To 80% – Yes.

  • WordPress & PHP : Different AdSense Units on Mobile Devices

    Here is How To Serve Different AdSense Units on Mobile Devices on WordPress With PHP. WordPress Has Function Which Can Be Used In Free Way.

  • MySQL Queries to Change WordPress HTTP to HTTPS

    Here are the required MySQL queries to change WordPress HTTP to HTTPS – including post images and all other contents. This is the right fix.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy