Query by term and custom field

$query = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->terms.slug = 'tvlt-notes'
AND $wpdb->term_taxonomy.taxonomy = 'tvlt_category'
AND $wpdb->postmeta.meta_key = 'tvlt_sort'
AND $wpdb->postmeta.meta_value <> '9999'

$tvlt_posts = $wpdb->get_results($query, OBJECT);

Count posts in taxonomy


global $wpdb;
$query = "
SELECT COUNT( DISTINCT cat_posts.ID ) AS post_count
FROM wp_term_taxonomy AS cat_term_taxonomy INNER JOIN wp_terms AS cat_terms ON
cat_term_taxonomy.term_id = cat_terms.term_id
INNER JOIN wp_term_relationships AS cat_term_relationships
ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
INNER JOIN wp_posts AS cat_posts
ON cat_term_relationships.object_id = cat_posts.ID
WHERE cat_posts.post_status = 'publish'
AND cat_posts.post_type = 'post'
AND cat_term_taxonomy.taxonomy = 'my_taxonomy'
AND cat_terms.slug IN ('term_slug','term-slug2')
return $wpdb->get_var($query);

Query for custom post taxonomy


$args = array(
'post_type' => 'my_custom_post_type',
'tax_query' => array(
'taxonomy' => 'my_taxonomy',
'field' => 'slug',
'terms' => 'my_custom_category_or_term'
$the_query = new WP_query();


//Loop Content

endwhile; endif;


Count children of a custom post

echo ezb_get_page_count();

function ezb_get_page_count(){
global $aquery,$wpdb;
$query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_parent = $aquery->ezb_id AND post_type = 'ezbook'";
$post_count = $wpdb->get_var($query);
return $post_count;

SQL Count Posts in Category


function count_posts($cat_id){
global $wpdb;

$query = "
SELECT COUNT(*) FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'private'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = $cat_id
return $wpdb->get_var($query);


Javascript Get Values

<script type="text/javascript">

alert(function jsGet('get-variable-name));

function jsGet(variable) {
var queryString = window.location.search.substring(1);
var vars = queryString.split("&");
for(var i=0; i<vars.length; i++){
var getValue = vars[i].split("=");
if(getValue[0] == variable) return getValue[1];


Build query issue


print http_build_query($data); // this sends the query string as var=myvar&amp;var2=myvar2 WONT work with as3

print http_build_query($data_str, '', '&'); // this sends the query string as var=myvar&var2=myvar2


Intercept Ajax Query

//Members query switch
function lmc_members_query($members_query){
case 'fans':
$members_query .= "&search_terms=Fan Account";
case 'bands':
$members_query .= "&search_terms=Band Account";
case 'venues':
$members_query .= "&search_terms=Venue Account";
return $members_query;
return $members_query;

//Add the above to your functions.php and then modify the bp loop within whatever area you want, for instance this is the modified version for the members loop:

<?php $members_query = lmc_members_query(bp_ajax_querystring('members'));?>

<?php if (bp_has_members($members_query)) :?>