Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Count custom post type by custom taxonomy
- $sql = "SELECT count(1) as post_count, t.name as status FROM $wpdb->posts p
- JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id)
- JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'home-status')
- JOIN $wpdb->terms t ON (tt.term_id = t.term_id)
- WHERE p.post_type = 'homes'
- AND (p.post_status = 'publish' OR p.post_status = 'private')
- AND p.post_date < NOW()
- GROUP BY t.name";
- $rows = $wpdb->get_results($sql, ARRAY_A);
- $counts = array();
- $labels = array('For Sale' => 'for sale', 'Sold' => 'sold');
- foreach ($rows as $row) {
- $counts[$row['status']] = $row['post_count'];
- }
- foreach (array_keys($labels) as $label) {
- $count = $counts[$labels[$label]];
- $text = ($count == 1) ? 'Home is' : 'Homes are';
- echo "<p>$count $text $label</p>";
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement