Skip to content

Instantly share code, notes, and snippets.

@ericpedia
Created August 24, 2011 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericpedia/1168471 to your computer and use it in GitHub Desktop.
Save ericpedia/1168471 to your computer and use it in GitHub Desktop.
AJAX taxonomy filter/search plugin file 1 of 2
<?php
// see the live version at http://bit.ly/q0qfqF
// click "Filter your results" to expand search panel
/**
* @package DTeam
* @version 1.0
*/
/*
Plugin Name: DTeam Advanced Search
Description: AJAX advanced search/taxonomy filter
Author: DTeam
Version: 1.0
Author URI:
*/
//add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'bio',
array(
'labels' => array(
'name' => __( 'Bio' ),
'singular_name' => __( 'Bio' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'bio')
)
);
register_post_type( 'clip',
array(
'labels' => array(
'name' => __( 'Clip' ),
'singular_name' => __( 'Clip' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'clip')
)
);
register_post_type( 'event',
array(
'labels' => array(
'name' => __( 'Event' ),
'singular_name' => __( 'Event' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'event')
)
);
register_post_type( 'feature',
array(
'labels' => array(
'name' => __( 'Feature' ),
'singular_name' => __( 'Feature' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'feature')
)
);
register_post_type( 'issue',
array(
'labels' => array(
'name' => __( 'Issue' ),
'singular_name' => __( 'Issue' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'issue')
)
);
register_post_type( 'news',
array(
'labels' => array(
'name' => __( 'News' ),
'singular_name' => __( 'News' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'news')
)
);
register_post_type( 'press',
array(
'labels' => array(
'name' => __( 'Press' ),
'singular_name' => __( 'Press' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'press')
)
);
register_post_type( 'publication',
array(
'labels' => array(
'name' => __( 'Publications' ),
'singular_name' => __( 'Publications' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'publication')
)
);
}
function dt_adv_search_func( $atts, $content=null ) {
extract( shortcode_atts( array(
'show' => '',
'preselect_people' => '',
'hide_people' => '',
'show_people' => '',
'preselect_issue' => '',
'hide_issue' => '',
'show_issue' => '',
'preselect_topic' => '',
'hide_topic' => '',
'show_topic' => '',
'preselect_type' => '',
'hide_type' => '',
'show_type' => '',
'search' => '',
'start_date' => '',
'end_date' => '',
'start_year' => '',
'end_year' => '',
), $atts ) );
$output = '';
$output .= '<link rel="stylesheet" type="text/css" href="'.get_option('siteurl').'/wp-content/plugins/dteam/css/dteam.css" />
<link rel="stylesheet" type="text/css" href="'.get_option('siteurl').'/wp-content/plugins/dteam/css/jquery.bsmselect.css" />
<link rel="stylesheet" type="text/css" href="'.get_option('siteurl').'/wp-content/plugins/dteam/css/jquery.rangeslider.css" />
<link rel="stylesheet" type="text/css" href="'.get_option('siteurl').'/wp-content/plugins/dteam/css/jquery.datepicker.css" />
<!-- <script type="text/javascript" src="'.get_option('siteurl').'/wp-content/plugins/dteam/js/jquery.min.js"></script> -->
<!-- <script type="text/javascript" src="'.get_option('siteurl').'/wp-content/plugins/dteam/js/jquery-ui.min.js"></script> -->
<script type="text/javascript" src="'.get_option('siteurl').'/wp-content/plugins/dteam/js/jquery-ui-1.8.14.custom.min.js"></script>
<script type="text/javascript" src="'.get_option('siteurl').'/wp-content/plugins/dteam/js/jquery.bsmselect.js"></script>
<!-- <script type="text/javascript" src="'.get_option('siteurl').'/wp-content/plugins/dteam/js/jquery.bsmselect.sortable.js"></script> -->
<script type="text/javascript" src="'.get_option('siteurl').'/wp-content/plugins/dteam/js/jquery.bsmselect.compatibility.js"></script>
<script type="text/javascript" src="'.get_option('siteurl').'/wp-content/plugins/dteam/js/jquery.blockUI.js"></script>
<script type="text/javascript" src="'.get_option('siteurl').'/wp-content/plugins/dteam/js/adv_pager.js"></script>';
$output .= '<link rel="stylesheet" type="text/css" href="'.get_option('siteurl').'/wp-content/plugins/dteam/ufd/css/ufd-base.css" />
<link rel="stylesheet" type="text/css" href="'.get_option('siteurl').'/wp-content/plugins/dteam/ufd/css/plain/plain.css" />
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="'.get_option('siteurl').'/wp-content/plugins/dteam/ufd/css/ufd-base-ie7.css" />
<![endif]-->';
$output .= '<script type="text/javascript" src="'.get_option('siteurl').'/wp-content/plugins/dteam/js/jquery.ui.ufd.js"></script>';
$output .= '<div id="adv-search-wrapper">';
$output .= '<div class="adv-search-header-wrapper">
<h2>
<a id="adv-search-toggle" class="adv-search-toggle">Filter your results</a>
</h2>
</div>';
$output .= '<div id="adv-search-form-wrapper" class="clearfix" style="display: none;">';
// $output .= '<div id="adv-search-form-wrapper" class="clearfix">';
$taxonomies = explode(',', $show);
$have_people = false;
$have_issue = false;
$have_topic = false;
$have_type = false;
$have_date = false;
$have_year = false;
$have_text = false;
$max_year = date('Y', time());
if (!$start_year) {
// $start_year = $max_year - 1;
$start_year = 1986;
}
if (!$end_year) {
$end_year = $max_year;
}
foreach($taxonomies as $taxonomy){
switch (trim($taxonomy)) {
case 'people':
$output .= getAdvSelect($taxonomy, $preselect_people, $show_people, $hide_people, 'By author', array('preselect_issue' => $preselect_issue, 'preselect_topic' => $preselect_topic, 'preselect_type' => $preselect_type), $start_year, $end_year, $start_date, $end_date, $search);
$have_people = true;
break;
case 'issue':
$output .= getAdvSelect($taxonomy, $preselect_issue, $show_issue, $hide_issue, 'By area of research', array('preselect_people' => $preselect_people, 'preselect_topic' => $preselect_topic, 'preselect_type' => $preselect_type), $start_year, $end_year, $start_date, $end_date, $search);
$have_issue = true;
break;
case 'topic':
$output .= getAdvSelect($taxonomy, $preselect_topic, $show_topic, $hide_topic, 'By topic', array('preselect_issue' => $preselect_issue, 'preselect_people' => $preselect_people, 'preselect_type' => $preselect_type), $start_year, $end_year, $start_date, $end_date, $search);
$have_topic = true;
break;
case 'type':
$output .= getAdvSelect($taxonomy, $preselect_type, $show_type, $hide_type, 'By type', array('preselect_issue' => $preselect_issue, 'preselect_topic' => $preselect_topic, 'preselect_people' => $preselect_people), $start_year, $end_year, $start_date, $end_date, $search);
$have_type = true;
break;
case 'date':
$output .= '<div class="date-slider-wrapper">
<div>
<h3 class="adv-heading">
By date:
</h3>
</div>
<div>
<h3 class="adv-heading">
Start date:
</h3>
<input type="text" id="start-date" name="start-date" />
<h3 class="adv-heading">
End date:
</h3>
<input type="text" id="end-date" name="end-date" />
</div>
</div>';
$have_date = true;
break;
case 'year':
$output .= '<div class="year-slider-wrapper clearfix">
<div>
<h3 class="adv-heading">
By year: <span id="year-slider-value"></span>
</h3>
<input type="hidden" id="min-year" name="min-year" />
<input type="hidden" id="max-year" name="max-year" />
</div>
<div id="year-slider"></div>
</div>';
$have_year = true;
break;
case 'text________Do_Not_Use':
$output .= '<div class="adv-search-text-wrapper">
<div>
<h3 class="adv-heading">
By content:
</h3>
</div>
<input type="text" id="adv-search-text" name="adv-search-text" class="" onfocus="if($(this).val() == \'Type and press enter\'){ $(this).val(\'\');$(this).removeClass(\'input-blurclass\'); $(this).addClass(\'myActiveClass\');}" onblur="if($(this).val() == \'\'){ $(this).val(\'Type and press enter\'); $(this).removeClass(\'myActiveClass\'); $(this).addClass(\'input-blurclass\');}" value="Type and press enter" />
</div>';
$have_text = true;
break;
case 'text':
$output .= '<div class="adv-search-text-wrapper">
<div>
<h3 class="adv-heading">
By content:
</h3>
</div>
<input type="text" id="adv-search-text" name="adv-search-text" class="text-input" value="Type and press enter" />
</div>';
$have_text = true;
break;
default:
}
}
if (!$have_people && !$have_issue && !$have_topic && !$have_type && !$have_date && !$have_year && !$have_text) {
$output .= 'You have not selected any items to show.
</div>';
}
else {
$output .= '</div>
<div id="adv-search-result-wrapper">
<br style="display:block; width:100%; clear:both; height: 4px;">
<!--<div id="adv-search-result-title">Search results</div>-->
<div id="adv-search-result" style="min-height: 100px;">';
$output .= '' . do_shortcode($content);
$output .= '</div>
</div>';
}
$output .= '</div>'; // div id="adv-search-wrapper"
$output .= '<script type="text/javascript">
//<![CDATA[
function getAdvSearchResults(taxonomy_type) {
var people = "";
var issue = "";
var topic = "";
var type = "";
var min_year = "";
var max_year = "";
var start_date = "";
var end_date = "";
var search_text = "";
var search = "&search='.$search.'";
';
if ($have_people)
$output .= ' if ($("#taxonomy-people").val())
people = "&people=" + $("#taxonomy-people").val();';
if ($have_issue)
$output .= ' if ($("#taxonomy-issue").val())
issue = "&issue=" + $("#taxonomy-issue").val();';
if ($have_topic)
$output .= ' if ($("#taxonomy-topic").val())
topic = "&topic=" + $("#taxonomy-topic").val();';
if ($have_type)
$output .= ' if ($("#taxonomy-type").val())
type = "&type=" + $("#taxonomy-type").val();';
if ($have_date)
$output .= ' start_date = "&start_date=" + $("#start-date").val();
end_date = "&end_date=" + $("#end-date").val();';
if ($have_year)
$output .= ' min_year = "&min_year=" + $("#min-year").val();
max_year = "&max_year=" + $("#max-year").val();';
if ($have_text)
$output .= ' if ($("#adv-search-text").val())
search_text = "&search_text=" + $("#adv-search-text").val();';
$output .= 'if (people || issue || topic || type || min_year || max_year || start_date || end_date || search_text) {
$("#adv-search-result").block({
message: "<img src=\''.get_option('siteurl').'/wp-content/plugins/dteam/css/images/ajax-loader.gif\' alt=\'\'>",
overlayCSS: {
backgroundColor: "#fff",
opacity: 0.4
},
css: {
padding: "24px",
margin: 0,
width: "30%",
top: "40%",
left: "35%",
textAlign: "center",
color: "#000",
border: "4px solid #aaa",
backgroundColor:"#fff",
cursor: "wait"
},
fadeIn: 200,
fadeOut: 500
});
$.ajax({
url: "'.get_option('siteurl').'/wp-content/plugins/dteam/ajax.php",
type: "POST",
data: "ajax=search" + people + issue + topic + type + min_year + max_year + start_date + end_date + search + search_text,
cache: false,
success: function(data) {
$("#adv-search-result").html(data);
$("#adv-search-result").unblock();
//$.getScript("'.get_option('siteurl').'/wp-content/plugins/dteam/js/adv_pager.js");
$("#adv-search-pagination a").each(function(){
var currlink = $(this).attr("href");
var pos = currlink.indexOf("paged=");
var paged = 1;
if (pos > 0) {
pos = pos+6;
if (currlink.indexOf("&", pos) > 0)
paged = currlink.substring(pos, currlink.indexOf("&", pos));
else
paged = currlink.substring(pos);
}
else {
pos = currlink.indexOf("/page/");
if (pos > 0) {
pos = pos+6;
if (currlink.indexOf("/", pos) > 0)
paged = currlink.substring(pos, currlink.indexOf("/", pos));
else
paged = currlink.substring(pos);
}
}
$(this).attr("href", "javascript:;");
$(this).click(function(){
getAdvSearchResultsPager(people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search, paged, \''.get_option('siteurl').'\');
});
});
}
});
}
else {
$("#adv-search-result").html("You do not choose any condition to search.");
}
if (taxonomy_type != "init") {
switch (taxonomy_type) {
case "people":';
if ($have_issue)
$output .= 'refresh_taxonomy("issue", "'.$show_issue.'", "'.$hide_issue.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
if ($have_topic)
$output .= 'refresh_taxonomy("topic", "'.$show_topic.'", "'.$hide_topic.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
if ($have_type)
$output .= 'refresh_taxonomy("type", "'.$show_type.'", "'.$hide_type.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
$output .= 'break;
case "issue":';
if ($have_people)
$output .= 'refresh_taxonomy("people", "'.$show_people.'", "'.$hide_people.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
if ($have_topic)
$output .= 'refresh_taxonomy("topic", "'.$show_topic.'", "'.$hide_topic.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
if ($have_type)
$output .= 'refresh_taxonomy("type", "'.$show_type.'", "'.$hide_type.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
$output .= 'break;
case "topic":';
if ($have_people)
$output .= 'refresh_taxonomy("people", "'.$show_people.'", "'.$hide_people.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
if ($have_issue)
$output .= 'refresh_taxonomy("issue", "'.$show_issue.'", "'.$hide_issue.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
if ($have_type)
$output .= 'refresh_taxonomy("type", "'.$show_type.'", "'.$hide_type.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
$output .= 'break;
case "type":';
if ($have_people)
$output .= 'refresh_taxonomy("people", "'.$show_people.'", "'.$hide_people.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
if ($have_issue)
$output .= 'refresh_taxonomy("issue", "'.$show_issue.'", "'.$hide_issue.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
if ($have_topic)
$output .= 'refresh_taxonomy("topic", "'.$show_topic.'", "'.$hide_topic.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
$output .= 'break;
default:';
if ($have_people)
$output .= 'refresh_taxonomy("people", "'.$show_people.'", "'.$hide_people.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
if ($have_issue)
$output .= 'refresh_taxonomy("issue", "'.$show_issue.'", "'.$hide_issue.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
if ($have_topic)
$output .= 'refresh_taxonomy("topic", "'.$show_topic.'", "'.$hide_topic.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);';
if ($have_type)
$output .= 'refresh_taxonomy("type", "'.$show_type.'", "'.$hide_type.'", people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search);
}
}
}
function refresh_taxonomy(taxonomy_type, show, hide, people, issue, topic, type, min_year, max_year, start_date, end_date, search_text, search) {
var select_title = "Select..."
switch (taxonomy_type) {
case "people":
select_title = "All authors";
break;
case "issue":
select_title = "All areas of research";
break;
case "topic":
select_title = "All topic";
break;
case "type":
select_title = "All types";
break;
}
$("#taxonomy-"+taxonomy_type+"-loader").html("<img src=\''.get_option('siteurl').'/wp-content/plugins/dteam/css/images/ajax-loader-1.gif\' alt=\'\'>");
$.ajax({
url: "'.get_option('siteurl').'/wp-content/plugins/dteam/ajax.php",
type: "POST",
data: "ajax=refresh_taxonomy&taxonomy_type=" + taxonomy_type + "&show=" + show + "&hide=" + hide + people + issue + topic + type + min_year + max_year + start_date + end_date + search + search_text,
cache: false,
success: function(data) {
$("#taxonomy-"+taxonomy_type+"-content").html(data);
$("#taxonomy-"+taxonomy_type+"-loader").html("");
$("#taxonomy-"+taxonomy_type).bsmSelect({
title: select_title,
removeLabel: "X",
removeClass: "remove-x",
addItemTarget: "bottom",
sortable: false,
animate: true,
highlight: true,
plugins: [
/* $.bsmSelect.plugins.sortable({ axis : "y", opacity : 0.5 }, { listSortableClass : "bsmListSortableCustom" }), */
$.bsmSelect.plugins.compatibility()
]
});';
// $output .= '$("select.bsmSelect.taxonomy-"+taxonomy_type).ufd({log:true});';
$output .= '$("select.bsmSelect.taxonomy-"+taxonomy_type).ufd({log:true, listMaxVisible:14, pageLength:14, addEmphasis:false, listWidthFixed:false });';
$output .= '}
});
}
function checkKeycode(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if(keycode == 13) getAdvSearchResults(\'text\');
}
$("#adv-search-form-wrapper").hide();
$("#adv-search-toggle").click(function(){
if ($(this).html() == "Filter your results") {
$("#adv-search-form-wrapper").removeClass("invisible")
$("#adv-search-form-wrapper").slideDown();
$(this).html("Close search form");
$(this).addClass("search-form-opened");
}
else {
$("#adv-search-form-wrapper").slideUp();
$(this).html("Filter your results");
$(this).removeClass("search-form-opened");
}
// $("#adv-search-toggle").click(function(){
// $("#adv-search-form-wrapper").slideToggle();
// $(this).toggleClass("search-form-opened");
});';
if ($have_people)
$output .= ' $("#taxonomy-people").bsmSelect({
title: "All authors",
removeLabel: "X",
removeClass: "remove-x",
addItemTarget: "bottom",
sortable: false,
animate: true,
highlight: true,
plugins: [
$.bsmSelect.plugins.compatibility()
]
});';
if ($have_issue)
$output .= ' $("#taxonomy-issue").bsmSelect({
title: "All areas of research",
removeLabel: "X",
removeClass: "remove-x",
addItemTarget: "bottom",
sortable: false,
animate: true,
highlight: true,
plugins: [
$.bsmSelect.plugins.compatibility()
]
});';
if ($have_topic)
$output .= ' $("#taxonomy-topic").bsmSelect({
title: "All topic",
removeLabel: "X",
removeClass: "remove-x",
addItemTarget: "bottom",
sortable: false,
animate: true,
highlight: true,
plugins: [
$.bsmSelect.plugins.compatibility()
]
});';
if ($have_type)
$output .= ' $("#taxonomy-type").bsmSelect({
title: "All types",
removeLabel: "X",
removeClass: "remove-x",
addItemTarget: "bottom",
sortable: false,
animate: true,
highlight: true,
plugins: [
$.bsmSelect.plugins.compatibility()
]
});';
if ($have_date)
$output .= ' var now = new Date();
var curr_date = now.getDate();
if (curr_date < 10) curr_date = "0" + curr_date;
var curr_month = now.getMonth();
curr_month++;
if (curr_month < 10) curr_month = "0" + curr_month;
var start_date = "'.$start_date.'";
var end_date = "'.$end_date.'";
if (start_date)
$("#start-date").val(start_date);
else
$("#start-date").val("2010" + "-" + curr_month + "-" + curr_date);
$("#start-date").datepicker({
changeMonth: true,
changeYear: true,
yearRange: "1986:'.$max_year.'",
dateFormat: "yy-mm-dd",
onSelect: function(dateText, inst) {
getAdvSearchResults(\'date\');
}
});
if (end_date)
$("#end-date").val(end_date);
else
$("#end-date").val(now.getFullYear() + "-" + curr_month + "-" + curr_date);
$("#end-date").datepicker({
changeMonth: true,
changeYear: true,
yearRange: "1986:'.$max_year.'",
dateFormat: "yy-mm-dd",
onSelect: function(dateText, inst) {
getAdvSearchResults(\'date\');
}
});';
if ($have_year)
$output .= ' $("#year-slider").slider({
range: true,
min: 1986,
max: '.$max_year.',
values: [ '.$start_year.', '.$end_year.' ],
stop: function( event, ui ) {
$( "#min-year" ).val(ui.values[ 0 ]);
$( "#max-year" ).val(ui.values[ 1 ]);
$( "#year-slider-value" ).html(ui.values[ 0 ] + " - " + ui.values[ 1 ]);
getAdvSearchResults(\'year\');
},
slide: function( event, ui ) {
$( "#year-slider-value" ).html(ui.values[ 0 ] + " - " + ui.values[ 1 ]);
}
});
$( "#min-year" ).val('.$start_year.');
$( "#max-year" ).val('.$end_year.');
$( "#year-slider-value" ).html("'.$start_year.' - '.$end_year.'");';
if ($have_text)
$output .= ' document.getElementById("adv-search-text").onkeydown = checkKeycode;';
// $output .= ' getAdvSearchResults(\'init\');';
$output .= ' //]]>
</script>
';
return $output;
}
function getAdvSelect($taxonomy='', $preselect='', $show='', $hide='', $title='', $other_preselect = array(), $min_year = '', $max_year = '', $start_date = '', $end_date = '', $search = '') {
global $wpdb;
$count_query = getTermCountQuery($taxonomy, $other_preselect, $min_year, $max_year, $start_date, $end_date, $search);
$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";
/****************************************************
This is where term order is set (for use with Gecka Term Order plugin)
****************************************************/
// if ($wpdb->termmeta) $querystr .= " left join $wpdb->termmeta tm on t.term_id = tm.term_id and meta_key = 'order'";
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 .= "\n" . '<div id="taxonomy-'.$taxonomy.'-wrapper" class="taxonomy-wrapper clearfix">';
$output .= "\n" . '<h3 class="adv-heading"><span id="taxonomy-'.$taxonomy.'-title" class="taxonomy-title">'.$title.': </span><span id="taxonomy-'.$taxonomy.'-loader"></span></h3>';
$output .= "\n" . '<div id="taxonomy-'.$taxonomy.'-content">';
$output .= "\n" . '<select id="taxonomy-'.$taxonomy.'" name="taxonomy-'.$taxonomy.'" class="taxonomy-select taxonomy-'.$taxonomy.'" multiple="multiple" onchange="getAdvSearchResults(\''.$taxonomy.'\');">';
foreach ($terms as $term) {
$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($term->name)), $shows))) {
if (empty($shows) || (!empty($shows) && in_array(strtolower(trim(str_replace(',', '', $term->name))), $shows))) {
if (!in_array(strtolower(trim(str_replace(',', '', $term->name))), $hides)) {
$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 = getHierarchicalTerms($arr_terms, $preselects, $term_id, $output_child, 1, $taxonomy, $other_preselect, $min_year, $max_year, $start_date, $end_date, $search);
$output_child .= $arr['output'];
if (strpos($arr['term_taxonomy_id'], "||") > 0) {
$count_query = getTermCountQuery($taxonomy, $other_preselect, $min_year, $max_year, $start_date, $end_date, $search, $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(strtolower(trim(str_replace(',', '', $arr_term['name']))), $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>';
$output .= '</div>';
$output .= '</div>';
return $output;
/****************************************************
adding a wrapper
****************************************************/
// return '<div class="advanced-search-wrapper clearfix">' . $output . '</div><!-- End of advanced-search-wrapper -->';
}
function getHierarchicalTerms($arr_terms = array(), $preselects = array(), $term_id = 0, $output = '', $hierarchical = 0, $taxonomy = '', $other_preselect = array(), $min_year = '', $max_year = '', $start_date = '', $end_date = '', $search = '') {
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 = getHierarchicalTerms($arr_terms, $preselects, $term_child_id, $output_child, $hierarchical + 1, $taxonomy, $other_preselect, $min_year, $max_year, $start_date, $end_date, $search);
$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 = getTermCountQuery($taxonomy, $other_preselect, $min_year, $max_year, $start_date, $end_date, $search, $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(strtolower(trim(str_replace(',', '', $arr_terms[$term_child_id]['name']))), $preselects)) ? ' selected="selected"' : '').'>'.getHierarchicalSymbol($hierarchical).$arr_terms[$term_child_id]['name'];
$output .= ' - ' . $count_result;
$output .= '</option>';
$output .= $output_child;
}
}
}
}
$arr['output'] = $output;
return $arr;
}
function getHierarchicalSymbol($hierarchical = 0) {
$i = 0;
$symbol = '';
while ($i<$hierarchical) {
$symbol .= ' - ';
$i++;
}
return $symbol;
}
function getTermCountQuery($taxonomy = '', $other_preselect = array(), $min_year = '', $max_year = '', $start_date = '', $end_date = '', $search = '', $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 ($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_relationships on p.ID = issue_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy issue_taxonomy on issue_relationships.term_taxonomy_id = issue_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms issue on issue_taxonomy.term_id = issue.term_id";
}
if ($other_preselect['preselect_topic']) {
$count_query .= " left join $wpdb->term_relationships topic_relationships on p.ID = topic_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy topic_taxonomy on topic_relationships.term_taxonomy_id = topic_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms topic on topic_taxonomy.term_id = topic.term_id";
}
if ($other_preselect['preselect_type']){
$count_query .= " left join $wpdb->term_relationships type_relationships on p.ID = type_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy type_taxonomy on type_relationships.term_taxonomy_id = type_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms type on type_taxonomy.term_id = type.term_id";
}
$count_query .= " where people.term_taxonomy_id".$term_id_condition;
if ($other_preselect['preselect_issue']){
$count_query .= " and issue.name in (";
$issue = explode(',', $other_preselect['preselect_issue']);
$i = 0;
foreach ($issue as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
if ($other_preselect['preselect_topic']) {
$count_query .= " and topic.name in (";
$topic = explode(',', $other_preselect['preselect_topic']);
$i = 0;
foreach ($topic as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
if ($other_preselect['preselect_type']){
$count_query .= " and type.name in (";
$type = explode(',', $other_preselect['preselect_type']);
$i = 0;
foreach ($type as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
$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_relationships on p.ID = people_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy people_taxonomy on people_relationships.term_taxonomy_id = people_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms people on people_taxonomy.term_id = people.term_id";
}
if ($other_preselect['preselect_topic']) {
$count_query .= " left join $wpdb->term_relationships topic_relationships on p.ID = topic_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy topic_taxonomy on topic_relationships.term_taxonomy_id = topic_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms topic on topic_taxonomy.term_id = topic.term_id";
}
if ($other_preselect['preselect_type']){
$count_query .= " left join $wpdb->term_relationships type_relationships on p.ID = type_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy type_taxonomy on type_relationships.term_taxonomy_id = type_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms type on type_taxonomy.term_id = type.term_id";
}
$count_query .= " where issue.term_taxonomy_id".$term_id_condition;
if ($other_preselect['preselect_people']){
$count_query .= " and people.name in (";
$people = explode(',', $other_preselect['preselect_people']);
$i = 0;
foreach ($people as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
if ($other_preselect['preselect_topic']) {
$count_query .= " and topic.name in (";
$topic = explode(',', $other_preselect['preselect_topic']);
$i = 0;
foreach ($topic as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
if ($other_preselect['preselect_type']){
$count_query .= " and type.name in (";
$type = explode(',', $other_preselect['preselect_type']);
$i = 0;
foreach ($type as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
$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_relationships on p.ID = issue_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy issue_taxonomy on issue_relationships.term_taxonomy_id = issue_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms issue on issue_taxonomy.term_id = issue.term_id";
}
if ($other_preselect['preselect_people']) {
$count_query .= " left join $wpdb->term_relationships people_relationships on p.ID = people_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy people_taxonomy on people_relationships.term_taxonomy_id = people_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms people on people_taxonomy.term_id = people.term_id";
}
if ($other_preselect['preselect_type']){
$count_query .= " left join $wpdb->term_relationships type_relationships on p.ID = type_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy type_taxonomy on type_relationships.term_taxonomy_id = type_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms type on type_taxonomy.term_id = type.term_id";
}
$count_query .= " where topic.term_taxonomy_id".$term_id_condition;
if ($other_preselect['preselect_issue']){
$count_query .= " and issue.name in (";
$issue = explode(',', $other_preselect['preselect_issue']);
$i = 0;
foreach ($issue as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
if ($other_preselect['preselect_people']) {
$count_query .= " and people.name in (";
$people = explode(',', $other_preselect['preselect_people']);
$i = 0;
foreach ($people as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
if ($other_preselect['preselect_type']){
$count_query .= " and type.name in (";
$type = explode(',', $other_preselect['preselect_type']);
$i = 0;
foreach ($type as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
$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_relationships on p.ID = issue_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy issue_taxonomy on issue_relationships.term_taxonomy_id = issue_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms issue on issue_taxonomy.term_id = issue.term_id";
}
if ($other_preselect['preselect_topic']) {
$count_query .= " left join $wpdb->term_relationships topic_relationships on p.ID = topic_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy topic_taxonomy on topic_relationships.term_taxonomy_id = topic_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms topic on topic_taxonomy.term_id = topic.term_id";
}
if ($other_preselect['preselect_people']){
$count_query .= " left join $wpdb->term_relationships people_relationships on p.ID = people_relationships.object_id";
$count_query .= " left join $wpdb->term_taxonomy people_taxonomy on people_relationships.term_taxonomy_id = people_taxonomy.term_taxonomy_id";
$count_query .= " left join $wpdb->terms people on people_taxonomy.term_id = people.term_id";
}
$count_query .= " where type.term_taxonomy_id".$term_id_condition;
if ($other_preselect['preselect_issue']){
$count_query .= " and issue.name in (";
$issue = explode(',', $other_preselect['preselect_issue']);
$i = 0;
foreach ($issue as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
if ($other_preselect['preselect_topic']) {
$count_query .= " and topic.name in (";
$topic = explode(',', $other_preselect['preselect_topic']);
$i = 0;
foreach ($topic as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
if ($other_preselect['preselect_people']){
$count_query .= " and people.name in (";
$people = explode(',', $other_preselect['preselect_people']);
$i = 0;
foreach ($people as $key=>$val) {
if($i > 0) $count_query .= ", ";
$count_query .= "'".trim($val)."'";
$i++;
}
$count_query .= ")";
}
$count_query .= $other_condition;
//}
break;
}
return $count_query;
}
add_shortcode( 'dt-adv-search', 'dt_adv_search_func' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment