Advanced Tax Query

$args = array(
'post_type' => 'actions',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
//'relation' => 'AND',
array(
'taxonomy' => 'actions_difficulty_levels',
'field' => 'slug',
'terms' => array($term)
),
array(
'taxonomy' => 'actions_tags',
'field' => 'slug',
'terms' => array('rebate'),
//'operator' => 'NOT IN'
)
)
);
$actions = get_posts($args);

Popular Posts Widget

/* ~~~~~~~~~~~ Popular Posts Widget ~~~~~~~~~~~ */

function register_ter_popular_posts(){ register_widget('TerPopularPosts'); }
add_action('widgets_init', 'register_ter_popular_posts');

function ter_set_post_views($id){
$count_key = 'ter_post_view_count';
$count = get_post_meta($id,$count_key,true);
if($count == ''){
$count = 0;
delete_post_meta($id,$count_key);
add_post_meta($id,$count_key,'0');
}
else{
$count++;
update_post_meta($id,$count_key,$count);
}
}

function ter_get_post_views($id){
$count_key = 'ter_post_view_count';
$count = get_post_meta($id,$count_key,true);
if($count == ''){
delete_post_meta($id,$count_key);
add_post_meta($id,$count_key,'0');
return '0 Views';
}
return $count . ' Views';
}

class TerPopularPosts extends WP_Widget{
function __construct() {
parent::__construct(
'ter_popular_posts',
__('Villages Popular Posts','terra'),
array('description' => __('The Popular Posts Widget','terra'),)
);
}

public function widget($args,$instance){
$title = apply_filters('widget_title',$instance['title']);
echo $args['before_widget'];
if(!empty($title)) echo $args['before_title'] . $title . $args['after_title'];

echo '<ul class="popular-posts">';
query_posts('meta_key=ter_post_view_count&orderby=meta_value_num&order=DESC&showposts=5');
if(have_posts()) : while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
<?php
endwhile; endif;
wp_reset_query();
echo '</ul>';

echo $args['after_widget'];
}

public function form($instance){
if(isset($instance['title'])) $title = $instance['title'];
else $title = __('Popular Posts','terra');
?>
<p>
<label for="<?php echo $this->get_field_id('title') ?>"><?php _e( 'Title:' ) ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title') ?>" name="<?php echo $this->get_field_name('title') ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p>
<?php
}

public function update($new_instance,$old_instance){
$instance = array();
$instance['title'] = (! empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
return $instance;
}

}//END TerPopularPosts

Custom Post Type Template

<?php
/*
Plugin Name: Hyp Coaches
Plugin URI: http://hyperspatial.com
Description: Custom post type for coaches
Author: Adam J Nowak
Version: 1.0
Author URI: http://hyperspatial.com
*/

//File Paths
define('HYPC_PLUGIN',WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/');
define('HYPC_PLUGIN_SERVERPATH',dirname(__FILE__) . '/');

$hyp_coaches = new HypCoaches();

class HypCoaches{
public function __construct(){
add_action('init',array(&$this,'init'));
add_filter('post_updated_messages',array(&$this,'updated_messages'));
//add_action('add_meta_boxes',array(&$this,'add_meta_boxes'));
//add_action('save_post',array(&$this,'save_post'));
}
public function init(){
$labels = array(
'name' => _x('Coaches','post type general name','terra'),
'singular_name' => _x('Coach','post type singular name','terra'),
'add_new' => _x('Add New','coach','terra'),
'add_new_item' => __('Add New Coach','terra'),
'edit_item' => __('Edit Coach','terra'),
'new_item' => __('New Coach','terra'),
'all_items' => __('All Coaches','terra'),
'view_item' => __('View Coach','terra'),
'search_items' => __('Search Coaches','terra'),
'not_found' => __('No coaches found','terra'),
'not_found_in_trash' => __('No coaches found in Trash','terra'),
'parent_item_colon' => '',
'menu_name' => __('Coaches','terra')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'menu_icon' => HYPC_PLUGIN . 'icon-coach.png',
'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields','revisions')
);
register_post_type('coach',$args);

//Taxonomy
$labels = array(
'name' => _x( 'Subjects', 'taxonomy general name' ),
'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
'search_items' => __( 'Search Subjects' ),
'all_items' => __( 'All Subjects' ),
'parent_item' => __( 'Parent Subject' ),
'parent_item_colon' => __( 'Parent Subject:' ),
'edit_item' => __( 'Edit Subject' ),
'update_item' => __( 'Update Subject' ),
'add_new_item' => __( 'Add New Subject' ),
'new_item_name' => __( 'New Subject Name' ),
'menu_name' => __( 'Subject' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
//'rewrite' => array( 'slug' => 'slugname' ),
);
register_taxonomy('subjects','coach',$args);
}

/* Updated Messages */
public function updated_messages($messages){
global $post, $post_ID;
$messages['coach'] = array(
0 => '',
1 => sprintf( __('Coach updated. <a href="%s">View coach</a>'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Coach updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Coach restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Coach published. <a href="%s">View coach</a>'), esc_url( get_permalink($post_ID) ) ),
7 => __('Coach saved.'),
8 => sprintf( __('Coach submitted. <a target="_blank" href="%s">Preview coach</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Coach scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview coach</a>'),
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __('Coach draft updated. <a target="_blank" href="%s">Preview post</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
}

/* Add meta box */
public function add_meta_boxes(){
add_meta_box(
'slug_name',
'Title Text',
array(&$this,'meta_box_1'),
'coach',
'advanced',
'high'
);
}

/* Meta box content */
public function meta_box_1($post){
wp_nonce_field(plugin_basename(__FILE__),'hyp_noncename');
$this->print_mce($post->ID,'metaname');
}

/* MCE */
private function print_mce($post_id,$metaname){
$content = get_post_meta($post_id,$metaname,1);
$wp_editor_args = array(
'teeny' => 1,
'textarea_rows' => 4,
'media_buttons' => true,
'dfw' => false,
'tinymce' => array(
'theme_advanced_buttons1' => 'formatselect,fontsizeselect,forecolor,backcolor|,bold,italic,underline,|,justifyleft,justifycenter,justifyright,|,undo,redo,|,spellchecker',
'theme_advanced_buttons2' => 'cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,|,fullscreen',
'theme_advanced_buttons3' => '',
'theme_advanced_buttons4' => ''
),
'quicktags' => true
);
wp_editor($content,$metaname . '_html',$wp_editor_args);
}

/* Save Postdata */
public function save_postdata($post_id){
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if($_POST['post_type'] != 'coach') return $post_id;
if(!wp_verify_nonce($_POST['hyp_noncename'],plugin_basename(__FILE__))) return $post_id;
if(!current_user_can('edit_post',$post_id)) return $post_id;
update_post_meta($post_id,'slug_name',$_POST['metaname_html']);
}
}//END HypCoach
?>

Assign post numbers

<?php
//Add this above the loop in your template
if(is_paged()) $post_number = $wp_query->found_posts - ($wp_query->query_vars['paged'] -1) * $wp_query->query_vars['posts_per_page'];
else $post_number = $wp_query->found_posts;
?>

<?php
//Add this within the loop wherever you want to print the number
echo $post_number; $post_number--;
?>

Query by term and custom field

<?php
$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

<?php

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);
?>

Count children of a custom post

<?php
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;
}
?>

Hide Private Posts

<?php
function hide_posts($where){
if( is_admin() ) return $where;
global $wpdb;
return " $where AND {$wpdb->posts}.post_status != 'private' ";
}

add_filter('posts_where','hide_posts');
?>