Skip to content

Instantly share code, notes, and snippets.

@christiangun
Created August 3, 2010 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christiangun/506224 to your computer and use it in GitHub Desktop.
Save christiangun/506224 to your computer and use it in GitHub Desktop.
<?php
/*
* This function is for Wordpress and will get post data from posts
* that are associated with a specific meta key and meta value pair.
*
* By: Christian Gundersson, christian@buro2.se
*
*/
function pcpt_get_posts_by_meta ( $meta_key, $meta_value )
{
global $wpdb;
$meta_table = $wpdb->prefix . "postmeta";
$post_table = $wpdb->prefix . "posts";
$sql = "SELECT * FROM " . $post_table . " LEFT JOIN " . $meta_table . "
ON " . $meta_table . ".post_id = " . $post_table . ".ID
WHERE meta_key = '" . $meta_key . "'
AND meta_value = '" . $meta_value . "'";
$post_results = $wpdb->get_results ( $sql );
return $post_results;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment