rendered paste body<?php
/*
Plugin Name: Custom field indexes
Plugin URI: http://www.davidsaccess.com/
Description: Functions for creating "nicer archives" type page for arbitrary custom fields.
Version: 1
Author: David Clark
Author URI: http://www.davidsaccess.com/
*/
function the_state_index($seperator = '',$target = 'index.php') {
$states = get_the_state();
$thelist = '';
if (sizeof($states) > 0) {
$thelist .= '';
foreach ($states as $state) {
$state->name = stripslashes($state->name);
$thelist .= "<li>";
$thelist .= '<a href = "'.get_settings('siteurl') .'/index.php?state='.$state->state_abbrev.'">';
$thelist .= $state->name. '</a></li>';
}
echo $thelist;
}
}
function the_state_select() {
global $tableposts, $tablepost2cat, $wpdb, $tablepostmeta;
$type = 7;
$states = $wpdb->get_results("SELECT abbrev, name
FROM states");
$thelist = '';
if (sizeof($states) > 0) {
$thelist .= '<select name = "state">';
$thelist .= '<option value="">Pick a state</option>';
foreach ($states as $state) {
$state->name = stripslashes($state->name);
$thelist .= "<option ";
if (get_query_var('state') == $state->abbrev) {$thelist .= 'SELECTED ';}
$thelist .= 'value = "'.$state->state_abbrev.'">';
$thelist .= $state->name. '</option>';
}
$thelist .= '</select>';
echo $thelist;
}
}
function get_the_state() {
global $post, $tablepostmeta, $wpdb;
$state = $wpdb->get_results("
SELECT meta_value as state_abbrev, name FROM $tablepostmeta, states
WHERE $tablepostmeta.meta_key = 'state'
AND $tablepostmeta.meta_value = states.abbrev
AND $tablepostmeta.post_id = $post->ID");
return $state;
}
function get_the_state_name($abbr) {
global $wpdb;
$state = $wpdb->get_var("SELECT name FROM `states` WHERE 1 AND abbrev = '$abbr'");
return $state;
}
function list_states(){
global $tableposts, $tablepost2cat, $wpdb, $tablepostmeta;
$target = get_settings('home') . '/index.php?state=' ;
$state_list = $wpdb->get_results("SELECT abbrev, name, COUNT( $wpdb->postmeta.meta_value ) AS state_count
FROM states
INNER JOIN $tablepostmeta ON ( abbrev = meta_value )
INNER JOIN $tableposts ON ($tableposts.ID = $tablepostmeta.post_id )
INNER JOIN $tablepost2cat ON ( $tableposts.ID = $tablepost2cat.post_id )
WHERE post_status = 'publish'
GROUP BY name");
$thelist = '';
if ($state_list) {
foreach ($state_list as $state) {
$link = '<a href="'.$target.$state->abbrev.'" ';
$link .= 'title="View all in '.$state->name.htmlspecialchars($state->abbrev) . '">';
$link .= $state->name.'</a>';
$link .= ' ('.$state->state_count.')';
$thelist .= '<li>'. $link . '</li>';
}
}
echo '<ul>'.$thelist.'</ul>';
}
function cat2catname($cat) {
if ($cat === '7') {$type_name = 'K-12 Schools';}
elseif ($cat === '8') {$type_name = 'Colleges';
}
return $type_name;
}
function list_state_posts($cat, $state) {
global $wpdb, $tableposts, $tablecategories, $tablepost2cat, $post, $tablepostmeta;
if ($state === '-1' )
{$where = ' ';}
else
{$where = "AND $tablepostmeta.meta_value = '$state'";}
$statelist = $wpdb->get_results("SELECT * FROM $tablepost2cat
LEFT JOIN $tableposts ON $tablepost2cat.post_id=$tableposts.ID
LEFT JOIN $tablepostmeta ON $tablepost2cat.post_id = $tablepostmeta.post_id
LEFT JOIN states ON states.abbrev = $tablepostmeta.meta_value
WHERE post_status = 'publish' AND category_id = $cat $where ORDER BY states.name, $tableposts.post_title ASC");
if ($statelist) {
foreach ($statelist as $state) {
$state_abbrev = $state->meta_value;
if ($state_abbrev != $previous) {
if ($i) {echo '</ul>';}
echo '<li class="list_heading"><a href="'.get_category_link(0, $cat, get_catname($cat)) .'&state='.$state_abbrev.'&full=1" rel="bookmark" title="'.get_catname($cat). ' in '.get_the_state_name($state_abbrev).'">'.get_the_state_name($state_abbrev).'</a></li>';
echo '<ul>';}
echo '<li><a href="'.get_permalink($state->ID).'" rel="bookmark" title="Permanent Link: '.$state->post_name.'">'.$state->post_title.'</a></li>';
$i = 1;
$previous = $state_abbrev;}
}
echo '</ul>';
}
function state_add_query_var($vars) {
// Append state.
$vars[] = 'state';
$vars[] = 'full';
// Don't forget to return the array.
return $vars;
}
function is_state() {
// Get the link category from WP_Query.
$link_category = get_query_var('state');
// If it's empty, return.
if (empty($link_category))
{return false;}
else {return true;}
}
function state_posts_where($where)
{
if (is_state())
$where = " AND wp_postmeta.meta_key = 'state' AND wp_postmeta.meta_value = '" . get_query_var('state')."'".$where;
return $where;
}
function postmeta_join($join)
{
if (is_state())
$join .= " LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)";
return $join;
}
function find_states($id) {
global $wpdb;
$content = $wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = '$id'");
if (preg_match_all('/<!--state=([\s\S]*?)-->/i', $content, $matches, PREG_SET_ORDER)) {
$test = $wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$id' AND meta_key = 'state'");
if (!empty($test)) {
$output = explode(' ', $test);
} else {
$output = array();
}
foreach($matches as $match) {
$output = array_merge($output, explode(' ', $match[1]));
}
$output = array_unique($output);
$keywords = implode(' ', $output);
if (!empty($test)) {
$results= $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$keywords' WHERE post_id = '$id' AND meta_key = 'state'");
} else {
$results = $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$id', 'state', '$keywords')");
}
$content = format_to_post(balanceTags(preg_replace("/<!--state=([\s\S]*?)-->/i", "<!--$1-->", $content)));
$results = $wpdb->query("UPDATE $wpdb->posts SET post_content = '$content' WHERE ID = '$id'");
}
return $id;
}
function order_query($query_string) {
global $doing_rss;
if (!empty($query_string) && !$doing_rss) {
$query_string .= '&orderby=title&order=asc&posts_per_page=99';
}
return $query_string;
}
function state_listing_template() {
if (is_state() && !is_category()) {
load_template(get_template_directory() . '/state.php');
exit;
}
}
add_action('template_redirect', 'state_listing_template', 1);
add_filter('query_string', 'order_query',1);
add_action('edit_post', 'find_states', 1);
add_action('publish_post', 'find_states', 1);
add_action('xmlrpc_methods', 'find_states', 1);
add_filter('query_vars', 'state_add_query_var');
add_filter('posts_where', 'state_posts_where');
add_filter('posts_join', 'postmeta_join');
?>