Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
master
Go to file
 
 
Cannot retrieve contributors at this time
230 lines (203 sloc) 7.08 KB
<?php
//
// Order search results by post_date
//
function my_search_query( $query ) {
// not an admin page and is the main query
if ( !is_admin() && $query->is_main_query() ) {
if ( is_search() ) {
$query->set( 'orderby', 'post_date' );
$query->set( 'order', 'DESC' );
}
}
}
add_action( 'pre_get_posts', 'my_search_query' );
function neag_scripts() {
wp_enqueue_style( 'neag-style', get_stylesheet_directory_uri() . '/css/neag.css');
wp_enqueue_script('neag-custom', get_stylesheet_directory_uri().'/js/custom.js', array('jquery', 'cs'));
wp_localize_script( 'neag-custom', 'neag_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'siteurl' => site_url() ) );
wp_enqueue_style( 'neag-print', get_stylesheet_directory_uri() .'/css/print.css', array( 'neag-style' ), false, 'print');
}
add_action( 'wp_enqueue_scripts', 'neag_scripts');
if ( function_exists('register_sidebar') ) {
register_sidebar( array(
'name' => __( 'Header - Social Media' ),
'id' => 'header',
'description' => __( 'Widgets in this area will be shown in the header.' ),
'before_title' => '<h1>',
'after_title' => '</h1>',
) );
register_sidebar( array(
'name' => __( 'Footer - Left' ),
'id' => 'footer-left',
'description' => __( 'Widgets in this area will be shown on the left hand side of the footer.' ),
'before_title' => '<h1>',
'after_title' => '</h1>',
) );
register_sidebar( array(
'name' => __( 'Footer - Center' ),
'id' => 'footer-center',
'description' => __( 'Widgets in this area will be shown on the right hand side of the footer.' ),
'before_title' => '<h1>',
'after_title' => '</h1>',
) );
register_sidebar( array(
'name' => __( 'Footer - Right' ),
'id' => 'footer-right',
'description' => __( 'Widgets in this area will be shown on the right hand side of the footer.' ),
'before_title' => '<h1>',
'after_title' => '</h1>',
) );
}
function neag_sort_by_last_then_first( $a, $b ){ // a and b are people IDs
setlocale(LC_CTYPE, 'en_US.UTF8'); // Using setlocale and iconv incase a name has an accent or other similar characters in them
$r = strnatcasecmp( iconv('UTF-8', 'ASCII//TRANSLIT', get_field('last_name', $a)), iconv('UTF-8', 'ASCII//TRANSLIT', get_field('last_name', $b)) );
if( $r === 0 ){
$r = strnatcasecmp( iconv('UTF-8', 'ASCII//TRANSLIT', get_field('first_name', $a)), iconv('UTF-8', 'ASCII//TRANSLIT', get_field('first_name', $b)) );
}
return $r;
}
function processPosts( $queryPosts, $expertsFlag = false ){
usort( $queryPosts, 'neag_sort_by_last_then_first' );
$fields = array( 'first_name' => '', 'middle_name' => '', 'last_name' => '', 'title' => '', 'email' => '', 'phone' => '', 'office_location' => '', 'expertise' => '' );
$alphabet = array_fill_keys( range('A', 'Z'), '' );
$people = array();
foreach( $queryPosts as $i=>$id ){
$people[$i] = array();
foreach( $fields as $k=>$v ){
$fields[$k] = get_field( $k, $id );
}
$people[$i]['name'] = "{$fields['first_name']} {$fields['middle_name']} {$fields['last_name']}";
$people[$i]['permalink'] = get_permalink( $id );
$people[$i]['phone'] = $fields['phone'];
$people[$i]['title'] = $fields['title'];
$people[$i]['email'] = $fields['email'];
$people[$i]['office_location'] = $fields['office_location'];
$people[$i]['photo'] = get_the_post_thumbnail( $id, array(115, 115) );
$people[$i]['expertise'] = $fields['expertise'];
$nameLetter = $fields['last_name'][0]; // $fields['last_name'] is a string
$setDivIDFlag = false;
if( empty( $alphabet[$nameLetter] ) ){
$setDivIDFlag = true;
$alphabet[$nameLetter] = true;
}
if( $setDivIDFlag == true ){
$people[$i]['divid'] = $nameLetter;
} else {
$people[$i]['divid'] = '';
}
}
return $people;
}
add_action( 'wp_ajax_neag_get_people', 'neag_get_people_callback' );
add_action( 'wp_ajax_nopriv_neag_get_people', 'neag_get_people_callback' );
function neag_get_people_callback(){
$categoryID = $_POST['categoryID'];
$args = array(
'post_type' => array( 'person' ),
'fields' => 'ids',
'posts_per_page' => -1,
);
if( $categoryID != 0 ){
$termSearch = array();
$termSearch['taxonomy'] = 'group';
$termSearch['field'] = 'term_id';
$termSearch['terms'] = $categoryID;
$taxQuery = array( $termSearch );
$args['tax_query'] = $taxQuery;
}
$query = new WP_Query( $args );
$queryPosts = $query->posts;
$people = array();
if( $query->have_posts() ){
$people = processPosts( $queryPosts );
}
echo wp_json_encode( $people );
wp_die();
}
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_person_neag',
'title' => 'Person',
'fields' => array (
array (
'key' => 'field_expertise',
'label' => 'Faculty Area of Expertise',
'name' => 'expertise',
'type' => 'text',
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'formatting' => 'html',
'maxlength' => '',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'person',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
//==================================================
// include authors in search results
//==================================================
/**
* Include posts from authors in the search results where
* either their display name or user login matches the query string
*
* @author danielbachhuber
*/
add_filter( 'posts_search', 'db_filter_authors_search' );
function db_filter_authors_search( $posts_search ) {
// Don't modify the query at all if we're not on the search template
// or if the LIKE is empty
if ( !is_search() || empty( $posts_search ) )
return $posts_search;
global $wpdb;
// Get all of the users of the blog and see if the search query matches either
// the display name or the user login
add_filter( 'pre_user_query', 'db_filter_user_query' );
$search = sanitize_text_field( get_query_var( 's' ) );
$args = array(
'count_total' => false,
'search' => sprintf( '*%s*', $search ),
'search_fields' => array(
'display_name',
'user_login',
),
'fields' => 'ID',
);
$matching_users = get_users( $args );
remove_filter( 'pre_user_query', 'db_filter_user_query' );
// Don't modify the query if there aren't any matching users
if ( empty( $matching_users ) )
return $posts_search;
// Take a slightly different approach than core where we want all of the posts from these authors
$posts_search = str_replace( ')))', ")) OR ( {$wpdb->posts}.post_author IN (" . implode( ',', array_map( 'absint', $matching_users ) ) . ") AND {$wpdb->posts}.post_type = 'post'))", $posts_search );
return $posts_search;
}
/**
* Modify get_users() to search display_name instead of user_nicename
*/
function db_filter_user_query( &$user_query ) {
if ( is_object( $user_query ) )
$user_query->query_where = str_replace( "user_nicename LIKE", "display_name LIKE", $user_query->query_where );
return $user_query;
}
?>
You can’t perform that action at this time.