Created
August 24, 2011 16:35
-
-
Save ericpedia/1168482 to your computer and use it in GitHub Desktop.
AJAX taxonomy filter/search plugin file 2 of 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('../../../wp-load.php'); | |
global $wpdb; | |
$ajax = isset($_POST['ajax']) ? $_POST['ajax'] : ''; | |
switch($ajax) { | |
case 'search': | |
$people = isset($_POST['people']) ? $_POST['people'] : ''; | |
$issue = isset($_POST['issue']) ? $_POST['issue'] : ''; | |
$topic = isset($_POST['topic']) ? $_POST['topic'] : ''; | |
$type = isset($_POST['type']) ? $_POST['type'] : ''; | |
$min_year = isset($_POST['min_year']) ? $_POST['min_year'] : ''; | |
$max_year = isset($_POST['max_year']) ? $_POST['max_year'] : ''; | |
$start_date = isset($_POST['start_date']) ? $_POST['start_date'] : ''; | |
$end_date = isset($_POST['end_date']) ? $_POST['end_date'] : ''; | |
$search = isset($_POST['search']) ? $_POST['search'] : ''; | |
$search_text = isset($_POST['search_text']) ? $_POST['search_text'] : ''; | |
$output = ''; | |
// $querystr = "SELECT distinct p.ID, p.post_title, p.post_date | |
// FROM $wpdb->posts p"; | |
/**************************************************** | |
Eric: Without p.post_type, the_permalink() wasn't working. | |
****************************************************/ | |
$querystr = "SELECT distinct p.ID, p.post_title, p.post_date, p.post_type, p.post_excerpt, p.post_status FROM $wpdb->posts p"; | |
if ($people) | |
$querystr .= " left join $wpdb->term_relationships people on p.ID = people.object_id"; | |
if ($issue) | |
$querystr .= " left join $wpdb->term_relationships issue on p.ID = issue.object_id"; | |
if ($topic) | |
$querystr .= " left join $wpdb->term_relationships topic on p.ID = topic.object_id"; | |
if ($type) | |
$querystr .= " left join $wpdb->term_relationships type on p.ID = type.object_id"; | |
$querystr .= " where 1 and p.post_status = 'publish'"; | |
if ($people) | |
$querystr .= " and people.term_taxonomy_id in (".str_replace('||',',',$people).")"; | |
if ($issue) | |
$querystr .= " and issue.term_taxonomy_id in (".str_replace('||',',',$issue).")"; | |
if ($topic) | |
$querystr .= " and topic.term_taxonomy_id in (".str_replace('||',',',$topic).")"; | |
if ($type) | |
$querystr .= " and type.term_taxonomy_id in (".str_replace('||',',',$type).")"; | |
if ($min_year) | |
$querystr .= " and SUBSTRING(p.post_date, 1, 4) >= '".$min_year."'"; | |
if ($max_year) | |
$querystr .= " and SUBSTRING(p.post_date, 1, 4) <= '".$max_year."'"; | |
if ($start_date) | |
$querystr .= " and SUBSTRING(p.post_date, 1, 10) >= '".$start_date."'"; | |
if ($end_date) | |
$querystr .= " and SUBSTRING(p.post_date, 1, 10) <= '".$end_date."'"; | |
if ($search) { | |
$post_types = explode(',', $search); | |
$querystr .= " and p.post_type in ("; | |
$i = 0; | |
foreach($post_types as $post_type) { | |
if ($i > 0) { | |
$querystr .= ","; | |
} | |
$querystr .= "'".trim($post_type)."'"; | |
$i++; | |
} | |
$querystr .= ")"; | |
} | |
else { | |
$querystr .= " and p.post_type not in ('revision', 'nav_menu_item')"; | |
} | |
if ($search_text && $search_text != "Type and press enter") | |
$querystr .= " and (p.post_title like '%".$search_text."%' or p.post_content like '%".$search_text."%')"; | |
$querystr .= " order by p.post_date desc"; | |
$totalposts = $wpdb->get_results($querystr, OBJECT); | |
$ppp = get_option('posts_per_page'); | |
$wp_query->found_posts = count($totalposts); | |
$wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp); | |
$on_page = isset($_POST['on_page']) ? $_POST['on_page'] : 1; | |
$offset = ($on_page-1) * $ppp; | |
$wp_query->set('paged', $on_page); | |
$wp_query->request = $querystr . " LIMIT " . $ppp . " OFFSET " .$offset; | |
$results = $wpdb->get_results($wp_query->request, OBJECT); | |
// $results = relevanssi_do_query($results); | |
global $post; // ERIC: this line and setup_postdata($post); below make template tags work | |
// http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query | |
foreach ($results as $post) { | |
setup_postdata($post); | |
$offset++; | |
?> | |
<?php get_template_part( 'loop-main' ); ?> | |
<?php | |
} | |
if (!$offset) { | |
$output .= 'No results found. Try a more general search.'; | |
} | |
//$output = $querystr; | |
echo $output; | |
echo '<p>Total results: '.count($totalposts).'</p>'; | |
echo '<div id="adv-search-pagination">'; | |
wp_pagenavi(); | |
echo '</div>'; | |
wp_reset_query(); | |
break; | |
case 'refresh_taxonomy': | |
$taxonomy = isset($_POST['taxonomy_type']) ? $_POST['taxonomy_type'] : ''; | |
$hide = isset($_POST['hide']) ? str_replace("\'", "'", $_POST['hide']) : ''; | |
$show = isset($_POST['show']) ? str_replace("\'", "'", $_POST['show']) : ''; | |
$people = isset($_POST['people']) ? $_POST['people'] : ''; | |
$issue = isset($_POST['issue']) ? $_POST['issue'] : ''; | |
$topic = isset($_POST['topic']) ? $_POST['topic'] : ''; | |
$type = isset($_POST['type']) ? $_POST['type'] : ''; | |
$min_year = isset($_POST['min_year']) ? $_POST['min_year'] : ''; | |
$max_year = isset($_POST['max_year']) ? $_POST['max_year'] : ''; | |
$start_date = isset($_POST['start_date']) ? $_POST['start_date'] : ''; | |
$end_date = isset($_POST['end_date']) ? $_POST['end_date'] : ''; | |
$search = isset($_POST['search']) ? $_POST['search'] : ''; | |
$search_text = isset($_POST['search_text']) ? $_POST['search_text'] : ''; | |
$other_preselect = array(); | |
switch ($taxonomy) { | |
case 'people': | |
$preselect = $people; | |
$other_preselect['preselect_issue'] = $issue; | |
$other_preselect['preselect_topic'] = $topic; | |
$other_preselect['preselect_type'] = $type; | |
break; | |
case 'issue': | |
$preselect = $issue; | |
$other_preselect['preselect_people'] = $people; | |
$other_preselect['preselect_topic'] = $topic; | |
$other_preselect['preselect_type'] = $type; | |
break; | |
case 'topic': | |
$preselect = $topic; | |
$other_preselect['preselect_issue'] = $issue; | |
$other_preselect['preselect_people'] = $people; | |
$other_preselect['preselect_type'] = $type; | |
break; | |
case 'type': | |
$preselect = $type; | |
$other_preselect['preselect_issue'] = $issue; | |
$other_preselect['preselect_topic'] = $topic; | |
$other_preselect['preselect_people'] = $people; | |
break; | |
} | |
global $wpdb; | |
$count_query = getTermCountQueryAjax($taxonomy, $other_preselect, $min_year, $max_year, $start_date, $end_date, $search, $search_text); | |
$querystr = "SELECT ta.term_taxonomy_id, t.name, t.slug, t.term_id, ta.parent, (".$count_query.") count_query | |
FROM $wpdb->term_taxonomy ta | |
inner join $wpdb->terms t on t.term_id = ta.term_id"; | |
if ($wpdb->termmeta) $querystr .= " left join $wpdb->termmeta tm on t.term_id = tm.term_id and meta_key = 'order'"; | |
$querystr .= " WHERE ta.taxonomy = '".$taxonomy."' | |
order by ta.parent"; | |
if ($wpdb->termmeta) $querystr .= ", tm.meta_value"; | |
$querystr .= ", t.name"; | |
//return $querystr.'<br><br><br>'; | |
$terms = $wpdb->get_results($querystr, OBJECT); | |
if ($show) { | |
$shows = explode(',', $show); | |
foreach ($shows as $key=>$val) { | |
$shows[$key] = strtolower(trim($val)); | |
} | |
} | |
else { | |
$shows = array(); | |
} | |
if ($hide) { | |
$hides = explode(',', $hide); | |
foreach ($hides as $key=>$val) { | |
$hides[$key] = strtolower(trim($val)); | |
} | |
} | |
else { | |
$hides = array(); | |
} | |
if ($preselect) { | |
$preselects = explode(',', $preselect); | |
foreach ($preselects as $key=>$val) { | |
$preselects[$key] = strtolower(trim($val)); | |
} | |
} | |
else { | |
$preselects = array(); | |
} | |
$arr_terms = array(); | |
$output = ''; | |
$output .= '<select id="taxonomy-'.$taxonomy.'" name="taxonomy-'.$taxonomy.'" class="taxonomy-select taxonomy-'.$taxonomy.'" multiple="multiple" onchange="getAdvSearchResults(\''.$taxonomy.'\');">'; | |
foreach ($terms as $term) { | |
if (!in_array(strtolower(trim(str_replace(',', '', $term->name))), $hides)) { | |
$arr_terms[$term->term_id]['term_taxonomy_id'] = $term->term_taxonomy_id; | |
if ($term->parent > 0) { | |
$arr_terms[$term->parent]['child'][] = $term->term_id; | |
} | |
if (empty($shows) || (!empty($shows) && in_array(strtolower(trim(str_replace(',', '', $term->name))), $shows))) { | |
$arr_terms[$term->term_id]['slug'] = $term->slug; | |
$arr_terms[$term->term_id]['name'] = $term->name; | |
$arr_terms[$term->term_id]['parent'] = $term->parent; | |
$arr_terms[$term->term_id]['count_query'] = $term->count_query; | |
} | |
} | |
} | |
if (!empty($arr_terms)) { | |
foreach ($arr_terms as $term_id=>$arr_term) { | |
if (isset($arr_term['term_taxonomy_id']) && isset($arr_term['name']) && $arr_term['parent'] == 0) { | |
$output_child = ''; | |
$arr = getHierarchicalTermsAjax($arr_terms, $preselects, $term_id, $output_child, 1, $taxonomy, $other_preselect, $min_year, $max_year, $start_date, $end_date, $search, $search_text); | |
$output_child .= $arr['output']; | |
if (strpos($arr['term_taxonomy_id'], "||") > 0) { | |
$count_query = getTermCountQueryAjax($taxonomy, $other_preselect, $min_year, $max_year, $start_date, $end_date, $search, $search_text, $arr['term_taxonomy_id']); | |
$count_result = $wpdb->get_var($count_query); | |
} | |
else { | |
$count_result = $arr_term['count_query']; | |
} | |
// $output .= '<option id="taxonomy-'.$taxonomy.'-'.$arr_term['slug'].'" value="'.$arr['term_taxonomy_id'].'"'.((in_array(strtolower(trim($arr_term['name'])), $preselects)) ? ' selected="selected"' : '').'>'.$arr_term['name'].'</option>'; | |
/**********************************************************************************/ | |
$output .= '<option id="taxonomy-'.$taxonomy.'-'.$arr_term['slug'] . '"'; | |
$output .= ' class="'. $taxonomy .' parent '. $arr_term['slug'] . ' level-0' . (($count_result > 0) ? '' : ' zero-result'); | |
/**********************************************************************************/ | |
$output .= '" value="'.$arr['term_taxonomy_id'].'"'.((in_array(trim($arr['term_taxonomy_id']), $preselects)) ? ' selected="selected"' : '') . (($taxonomy == 'people' && $arr_term['name'] == 'Others') ? ' disabled="disabled"' : ''); | |
$output .= '>'.$arr_term['name']; | |
$output .= ' - ' . $count_result; | |
$output .= '</option>' . "\n"; | |
$output .= $output_child; | |
} | |
} | |
} | |
$output .= '</select>'; | |
//echo $querystr.'<br><br><br>'; | |
echo $output; | |
break; | |
default: | |
$output = 'None'; | |
echo $output; | |
} | |
function getHierarchicalTermsAjax($arr_terms = array(), $preselects = array(), $term_id = 0, $output = '', $hierarchical = 0, $taxonomy = '', $other_preselect = array(), $min_year = '', $max_year = '', $start_date = '', $end_date = '', $search = '', $search_text = '') { | |
global $wpdb; | |
$arr = array(); | |
$arr['term_taxonomy_id'] = $arr_terms[$term_id]['term_taxonomy_id']; | |
if (!empty($arr_terms[$term_id]['child'])) { | |
foreach ($arr_terms[$term_id]['child'] as $term_child_id) { | |
if (isset($arr_terms[$term_child_id]['term_taxonomy_id'])) { | |
$output_child = ''; | |
$arr_child = getHierarchicalTermsAjax($arr_terms, $preselects, $term_child_id, $output_child, $hierarchical + 1, $taxonomy, $other_preselect, $min_year, $max_year, $start_date, $end_date, $search, $search_text); | |
$arr['term_taxonomy_id'] .= '||' . $arr_child['term_taxonomy_id']; | |
if (isset($arr_terms[$term_child_id]['name'])) { | |
$output_child .= $arr_child['output']; | |
if (strpos($arr_child['term_taxonomy_id'], "||") > 0) { | |
$count_query = getTermCountQueryAjax($taxonomy, $other_preselect, $min_year, $max_year, $start_date, $end_date, $search, $search_text, $arr_child['term_taxonomy_id']); | |
$count_result = $wpdb->get_var($count_query); | |
} | |
else { | |
$count_result = $arr_terms[$term_child_id]['count_query']; | |
} | |
$output .= '<option id="taxonomy-'.$taxonomy.'-'.$arr_terms[$term_child_id]['slug']; | |
$output .= '" class="'. $taxonomy . ' child ' . $arr_terms[$term_child_id]['slug'] . ' level-'.$hierarchical . ' parent-'.$term_id . (($count_result > 0) ? '' : ' zero-result'); | |
/**********************************************************************************/ | |
// if ($taxonomy) { | |
// $term_object = get_term_by( 'id', $arr_term['term_taxonomy_id'], 'people', OBJECT); | |
// $output .= print_r($term_object->parent); | |
// } | |
/**********************************************************************************/ | |
$output .= '" value="'.$arr_child['term_taxonomy_id'].'"'.((in_array(trim($arr_child['term_taxonomy_id']), $preselects)) ? ' selected="selected"' : '').'>'.getHierarchicalSymbolAjax($hierarchical).$arr_terms[$term_child_id]['name']; | |
$output .= ' - ' . $count_result; | |
$output .= '</option>'; | |
$output .= $output_child; | |
} | |
} | |
} | |
} | |
$arr['output'] = $output; | |
return $arr; | |
} | |
function getHierarchicalSymbolAjax($hierarchical = 0) { | |
$i = 0; | |
$symbol = ''; | |
while ($i<$hierarchical) { | |
$symbol .= ' - '; | |
$i++; | |
} | |
return $symbol; | |
} | |
function getTermCountQueryAjax($taxonomy = '', $other_preselect = array(), $min_year = '', $max_year = '', $start_date = '', $end_date = '', $search = '', $search_text= '', $term_id = '') { | |
global $wpdb; | |
$count_query = ''; | |
$other_condition = " and p.post_status = 'publish'"; | |
$term_id_condition = ''; | |
if ($min_year) | |
$other_condition .= " and SUBSTRING(p.post_date, 1, 4) >= '".$min_year."'"; | |
if ($max_year) | |
$other_condition .= " and SUBSTRING(p.post_date, 1, 4) <= '".$max_year."'"; | |
if ($start_date) | |
$other_condition .= " and SUBSTRING(p.post_date, 1, 10) >= '".$start_date."'"; | |
if ($end_date) | |
$other_condition .= " and SUBSTRING(p.post_date, 1, 10) <= '".$end_date."'"; | |
if ($search) { | |
$post_types = explode(',', $search); | |
$other_condition .= " and p.post_type in ("; | |
$i = 0; | |
foreach($post_types as $post_type) { | |
if ($i > 0) { | |
$other_condition .= ","; | |
} | |
$other_condition .= "'".trim($post_type)."'"; | |
$i++; | |
} | |
$other_condition .= ")"; | |
} | |
else { | |
$other_condition .= " and p.post_type not in ('revision', 'nav_menu_item')"; | |
} | |
if ($search_text && $search_text != "Type and press enter") | |
$other_condition .= " and (p.post_title like '%".$search_text."%' or p.post_content like '%".$search_text."%')"; | |
if ($term_id) | |
$term_id_condition .= " in (".str_replace('||', ',', $term_id).")"; | |
else | |
$term_id_condition .= " = ta.term_taxonomy_id"; | |
switch($taxonomy){ | |
case 'people': | |
//if ($other_preselect['preselect_issue'] || $other_preselect['preselect_topic'] || $other_preselect['preselect_type']) { | |
$count_query = "select count(distinct p.ID) from $wpdb->posts p | |
left join $wpdb->term_relationships people on p.ID = people.object_id"; | |
if ($other_preselect['preselect_issue']){ | |
$count_query .= " left join $wpdb->term_relationships issue on p.ID = issue.object_id"; | |
} | |
if ($other_preselect['preselect_topic']) { | |
$count_query .= " left join $wpdb->term_relationships topic on p.ID = topic.object_id"; | |
} | |
if ($other_preselect['preselect_type']){ | |
$count_query .= " left join $wpdb->term_relationships type on p.ID = type.object_id"; | |
} | |
$count_query .= " where people.term_taxonomy_id".$term_id_condition; | |
if ($other_preselect['preselect_issue']){ | |
$count_query .= " and issue.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_issue']).")"; | |
} | |
if ($other_preselect['preselect_topic']) { | |
$count_query .= " and topic.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_topic']).")"; | |
} | |
if ($other_preselect['preselect_type']){ | |
$count_query .= " and type.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_type']).")"; | |
} | |
$count_query .= $other_condition; | |
//} | |
break; | |
case 'issue': | |
//if ($other_preselect['preselect_people'] || $other_preselect['preselect_topic'] || $other_preselect['preselect_type']) { | |
$count_query = "select count(distinct p.ID) from $wpdb->posts p | |
left join $wpdb->term_relationships issue on p.ID = issue.object_id"; | |
if ($other_preselect['preselect_people']){ | |
$count_query .= " left join $wpdb->term_relationships people on p.ID = people.object_id"; | |
} | |
if ($other_preselect['preselect_topic']) { | |
$count_query .= " left join $wpdb->term_relationships topic on p.ID = topic.object_id"; | |
} | |
if ($other_preselect['preselect_type']){ | |
$count_query .= " left join $wpdb->term_relationships type on p.ID = type.object_id"; | |
} | |
$count_query .= " where issue.term_taxonomy_id".$term_id_condition; | |
if ($other_preselect['preselect_people']){ | |
$count_query .= " and people.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_people']).")"; | |
} | |
if ($other_preselect['preselect_topic']) { | |
$count_query .= " and topic.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_topic']).")"; | |
} | |
if ($other_preselect['preselect_type']){ | |
$count_query .= " and type.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_type']).")"; | |
} | |
$count_query .= $other_condition; | |
//} | |
break; | |
case 'topic': | |
//if ($other_preselect['preselect_issue'] || $other_preselect['preselect_people'] || $other_preselect['preselect_type']) { | |
$count_query = "select count(distinct p.ID) from $wpdb->posts p | |
left join $wpdb->term_relationships topic on p.ID = topic.object_id"; | |
if ($other_preselect['preselect_issue']){ | |
$count_query .= " left join $wpdb->term_relationships issue on p.ID = issue.object_id"; | |
} | |
if ($other_preselect['preselect_people']) { | |
$count_query .= " left join $wpdb->term_relationships people on p.ID = people.object_id"; | |
} | |
if ($other_preselect['preselect_type']){ | |
$count_query .= " left join $wpdb->term_relationships type on p.ID = type.object_id"; | |
} | |
$count_query .= " where topic.term_taxonomy_id".$term_id_condition; | |
if ($other_preselect['preselect_issue']){ | |
$count_query .= " and issue.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_issue']).")"; | |
} | |
if ($other_preselect['preselect_people']) { | |
$count_query .= " and people.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_people']).")"; | |
} | |
if ($other_preselect['preselect_type']){ | |
$count_query .= " and type.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_type']).")"; | |
} | |
$count_query .= $other_condition; | |
//} | |
break; | |
case 'type': | |
//if ($other_preselect['preselect_issue'] || $other_preselect['preselect_topic'] || $other_preselect['preselect_people']) { | |
$count_query = "select count(distinct p.ID) from $wpdb->posts p | |
left join $wpdb->term_relationships type on p.ID = type.object_id"; | |
if ($other_preselect['preselect_issue']){ | |
$count_query .= " left join $wpdb->term_relationships issue on p.ID = issue.object_id"; | |
} | |
if ($other_preselect['preselect_topic']) { | |
$count_query .= " left join $wpdb->term_relationships topic on p.ID = topic.object_id"; | |
} | |
if ($other_preselect['preselect_people']){ | |
$count_query .= " left join $wpdb->term_relationships people on p.ID = people.object_id"; | |
} | |
$count_query .= " where type.term_taxonomy_id".$term_id_condition; | |
if ($other_preselect['preselect_issue']){ | |
$count_query .= " and issue.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_issue']).")"; | |
} | |
if ($other_preselect['preselect_topic']) { | |
$count_query .= " and topic.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_topic']).")"; | |
} | |
if ($other_preselect['preselect_people']){ | |
$count_query .= " and people.term_taxonomy_id in (".str_replace('||', ',', $other_preselect['preselect_people']).")"; | |
} | |
$count_query .= $other_condition; | |
//} | |
break; | |
} | |
return $count_query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment