Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP WordPress Get All Custom Fields

WordPress Get All Custom Fields

This article shows a piece of PHP code that query all Custom Fields (extra fields or meta data) of a particular post instead of hitting the WordPress database for every custom field.

Assume that you’ve added a lot of custom fields or extra fields for each of post, pulling all meta data in one request will reduce connections to the database and will significant improve your WordPress website performance.

1. PHP Get All Custom Fields For A Single Post In WordPress

The PHP script below will query and store all custom fields for the current post in an Array. And then we can easily read value of these custom fields such as featured_image, demonstration_url, download_url, etc.

<?php
	$post_id = $post->ID;
 
	global $wpdb;
 
	$post_meta_data = array();
 
	$wpdb->query("
		SELECT `meta_key`, `meta_value`
		FROM $wpdb->postmeta
		WHERE `post_id` = $post_id
	");
 
	foreach($wpdb->last_result as $k => $v){
		$post_meta_data[$v->meta_key] = $v->meta_value;
	};
 
	$featured_image = $post_meta_data["featured_image"];
	$disable_meta = $post_meta_data["disable_meta"];
	$center_ads = $post_meta_data["center_ads"];
	$demonstration_url = $post_meta_data["demonstration_url"];
	$download_url = $post_meta_data["download_url"];
?>

<?php $post_id = $post->ID; global $wpdb; $post_meta_data = array(); $wpdb->query(" SELECT `meta_key`, `meta_value` FROM $wpdb->postmeta WHERE `post_id` = $post_id "); foreach($wpdb->last_result as $k => $v){ $post_meta_data[$v->meta_key] = $v->meta_value; }; $featured_image = $post_meta_data["featured_image"]; $disable_meta = $post_meta_data["disable_meta"]; $center_ads = $post_meta_data["center_ads"]; $demonstration_url = $post_meta_data["demonstration_url"]; $download_url = $post_meta_data["download_url"]; ?>

You can place this PHP script right in the loop-single.php file in your WordPress template folder or create a function that returns all custom field for a single post then place it in your WordPress functions.php file as below:

2. Function returns all custom field in WordPress

<?php
	function get_all_post_custom_fields($post_id){
		global $wpdb;
 
		$post_meta_data = array();
 
		$wpdb->query("
			SELECT `meta_key`, `meta_value`
			FROM $wpdb->postmeta
			WHERE `post_id` = $post_id
		");
 
		foreach($wpdb->last_result as $k => $v){
			$post_meta_data[$v->meta_key] =   $v->meta_value;
		};
 
		return $post_meta_data;
	}
?>

<?php function get_all_post_custom_fields($post_id){ global $wpdb; $post_meta_data = array(); $wpdb->query(" SELECT `meta_key`, `meta_value` FROM $wpdb->postmeta WHERE `post_id` = $post_id "); foreach($wpdb->last_result as $k => $v){ $post_meta_data[$v->meta_key] = $v->meta_value; }; return $post_meta_data; } ?>

Apr 15, 2012Hoan Huynh
Share Link/ Url Of Facebook, Twitter, Google Plus, Stumbleupon, Reddit, Digg and TumblrDisplay Technorati Media Ads In Iframe
You Might Also Like:
  • Add And Get Custom Field In WordPress
  • WordPress Check If Post Is In Category
  • Get WordPress Most Popular Posts By Total Comments
  • Show Error Message On WordPress Custom Login Template Page
  • Create Custom Update Profile Page For WordPress Users
  • How To Get WordPress Tags By Categories
  • Delete Bulk Draft And Trash WordPress Posts Includes All Tags, Comments, Meta Fields And Terms Associated
  • Add WordPress Login Form On Sidebar Or Custom Page
  • How to create new custom menu item in your wordpress back end?
  • A handy guideline on adding custom menu item in WordPress admin
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

8 years ago PHPCustom Field, postmeta, Wordpress186
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
21,931 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
19,760 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,596 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,112 views
C# Read Json From URL And Parse/Deserialize Json
9,565 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Essay Writing Service Tips For The Online Essay
  • Research Paper Writing Service
  • Annotated Bibliography Example – How it Can Help You
  • Essay Writing Online Tips – How to Write Essays Online With Essay Proof Editing
  • Get Research Paper Assistance From Professional Help
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (64)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development