I need to find if a post with a custom field X equal to Y exists in a wordpress installation.

Should I do it with a simple sql query or is there something build in that can help me achieve it?

  • You can do this either at the PHP or SQL level. How against JOINs are you? Or perhaps more appropriately, how big is your data?
    – editor
    Feb 14, 2011 at 20:27
  • I expect to have about 40 to 80k posts, each one with a custom field that would be like an unique ID and will help me to not create duplicated posts.
    – user3148
    Feb 14, 2011 at 20:40

3 Answers 3


You can use the WP_Query(); to check that like this:

$my_query = new WP_Query();
$my_query->query(array( 'meta_key' => 'X', 'meta_value' => 'Y'));
if ( $my_query->have_posts() ){

     //it exists

} else {
    //it's not here

Hope this helps.

  • That looks good, but which files do I need to include to use it? Because I need to use it from a .php that will run as cronjob, not from inside a plugin admin panel or something like that.
    – user3148
    Feb 14, 2011 at 20:02
  • 1
    @Rocxy this will require good chunk of WP core. Most compatible would be to use WP-Cron to run it inside WP, otherwise see how to load WP core in external code.
    – Rarst
    Feb 14, 2011 at 20:06
  • you can wrap in in a function and return a Boolean true or false if the post exists and add this to your plugin file.
    – Bainternet
    Feb 14, 2011 at 20:07
  • WP-Cron is no good for this case because the cronjob does some really extensive tasks and AFAIK wp-cron add the load time of the cron to the website. I will see if I can load WPCore.
    – user3148
    Feb 14, 2011 at 20:12
  • add post_type to the query for custom post types Jun 9, 2021 at 5:58

The current way would be something like this:

$posts = get_posts(array(
    'meta_key' => 'color',
    'meta_value' => 'blue',

if( !empty($posts) ) { // some posts found }

Just note that in upcoming 3.1 querying for custom fields is considerably improved and arguments change, see Custom Field Parameters in Codex.

  • Same question as for the other answers, which files should I include to use this from a stand-alone php file that would run as cronjob?
    – user3148
    Feb 14, 2011 at 20:06

If you want to use this externally(per your comments), to keep the operation cheap you could do something like this.

// You'll need to make sure the path is correct here
include_once 'wp-config.php';
include_once 'wp-includes/wp-db.php';

// Of course update the following line with a more appropriate query
$result = $wpdb->get_results("SELECT * FROM $wpdb->posts"); 

print '<pre>';
print_r( $result ); // Remove and do whatever you need to do
print '</pre>';

Loading wp-config.php and wp-db.php should be enough to call upon the $wpdb methods to query the DB for whatever data you need.

Let me know if you need an example SQL query to fetch posts based on meta.

  • Cheaper still would be to just use mysql_query directly, but you'd then have to deal with opening and closing the connections yourself, etc...
    – t31os
    Mar 8, 2011 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy