Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Created October 26, 2010 05:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeschinkel/646399 to your computer and use it in GitHub Desktop.
Save mikeschinkel/646399 to your computer and use it in GitHub Desktop.
Query for a list of WordPress posts by category, but only those with a postmeta->meta_key='Price' that's >0 and NOT NULL.
<?php
/*
posts-by-category-with-price.php
Query for a list of WordPress posts by category,
but only those with a postmeta->meta_key='Price' that's >0 and NOT NULL.
Author: Mike Schinkel (http://mikeschinkel.com)
Just drop this example into the root of your website and call directly to see it work.
Use the class in your theme's functions.php file, or a .php file of a plugin you are writing.
In Answer To: http://wordpress.stackexchange.com/questions/3291/
*/
include "../wp-load.php";
$category = PostsByCategoryWithPrice::get_category(0);
$posts = PostsByCategoryWithPrice::query();
$column=0;
foreach($posts as $post) {
if ($column++>6)
$column = 1;
if ($category->id != $post->category_id) {
$category = PostsByCategoryWithPrice::get_category($post); ?>
<h2 style="clear:both;">
<a href="<?php echo get_category_link($category->id); ?>">
<?php echo($category->name); ?></a>
</h2><?php
}?>
<div class="span-1 thumb20<?php echo ($count==6 ? ' last' : ''); ?>">
<a class="thumb" href="<?php the_permalink(); ?>" rel="bookmark"
title="Link to <?php the_title(); ?>">
<?php echo the_post_thumbnail( 'thumbnail' ); ?>
</a>
</div><?php
}?>
<hr class="gallery-grid" />
<?php
class PostsByCategoryWithPrice {
static $hooks = array();
private static function push_action($hook,$callable,$priority=10,$params=1) {
self::$hooks[$hook] = $callable;
add_action($hook,$callable,$priority,$params);
}
private static function pop_action($count=1) {
for($i=$count; $i>0; $i--) {
$hook = end(array_keys(self::$hooks));
$callable = array_pop(self::$hooks);
remove_action($hook,$callable);
}
}
static function get_category($post=false) {
if (!$post)
$category = (object)array('id'=>false);
else
$category = (object)array(
'id' => $post->category_id,
'name' => $post->category_name,
'slug' => $post->category_slug,
);
return $category;
}
static function query() {
self::push_action('posts_fields',array(__CLASS__,'posts_fields'));
self::push_action('posts_join',array(__CLASS__,'posts_join'));
self::push_action('posts_where',array(__CLASS__,'posts_where'));
self::push_action('posts_orderby',array(__CLASS__,'posts_orderby'));
$query = new WP_Query("posts_per_page=-1");
self::pop_action(4);
return $query->posts;
}
static function posts_fields($fields) {
global $wpdb;
$fields .= ",{$wpdb->terms}.term_id AS category_id,
{$wpdb->terms}.name AS category_name,
{$wpdb->terms}.slug AS category_slug";
return $fields;
}
static function posts_join($join) {
global $wpdb;
$join .=<<<SQL
INNER JOIN {$wpdb->postmeta}
ON {$wpdb->postmeta}.post_id={$wpdb->posts}.ID
AND {$wpdb->postmeta}.meta_key='price'
INNER JOIN {$wpdb->term_relationships}
ON {$wpdb->term_relationships}.object_id={$wpdb->posts}.ID
INNER JOIN {$wpdb->term_taxonomy}
ON {$wpdb->term_taxonomy}.term_taxonomy_id={$wpdb->term_relationships}.term_taxonomy_id
INNER JOIN {$wpdb->terms}
ON {$wpdb->term_taxonomy}.term_id={$wpdb->terms}.term_id
SQL;
return $join;
}
static function posts_where($where) {
global $wpdb;
$where .=" AND {$wpdb->term_taxonomy}.taxonomy='category'
AND IFNULL({$wpdb->postmeta}.meta_value,'0')!='0' ";
return $where;
}
static function posts_orderby($orderby) {
global $wpdb;
$orderby = " {$wpdb->term_taxonomy}.name,
{$wpdb->posts}.post_title ASC ";
return $orderby;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment