Plugin Directory

Changeset 1460817


Ignore:
Timestamp:
07/26/2016 12:52:53 PM (10 years ago)
Author:
directict
Message:

#2 When no CustomFields selected we return the default WP Search where-clause

Location:
acf-search/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • acf-search/trunk/acf-search.php

    r1460094 r1460817  
    55Description: Adds the ability to search trough the selected ACF-fields
    66Author: Direct ICT B.V.
    7 Version: 2.1.4
     7Version: 2.1.5
    88Author URI: https://www.directict.nl/
    99*/
    1010
    1111// how da fuck -.-
    12 global $acf_fields;
    13 $acf_fields = array();
     12global $acf_search_fields;
     13$acf_search_fields = array();
    1414
    15 global $plugin_data;
     15global $acf_search_plugin_data;
    1616
    17 function acf_search_init() {   
    18     global $acf_fields;
     17function acf_search_init() {
     18    global $acf_search_fields;
    1919    $t=array();
    2020    add_filter( 'posts_where' , 'posts_where_excerpt_not_empty' );
    2121    $ACFFIELDS = new WP_Query(
    22         array( 
     22        array(
    2323            "posts_per_page" => -1,
    2424            "post_type" => 'acf-field'
     
    2626    );
    2727    remove_filter( 'posts_where' , 'posts_where_excerpt_not_empty' );
    28    
     28
    2929    while($ACFFIELDS->have_posts()) {
    3030        $ACFFIELDS->the_post();
    3131        if(!in_array(get_the_excerpt(), $t)) {
    32             if(get_the_excerpt()) { 
    33                 $acf_fields[] = (object)array("display" => get_the_title(), "return" => get_the_excerpt());
     32            if(get_the_excerpt()) {
     33                $acf_search_fields[] = (object)array("display" => get_the_title(), "return" => get_the_excerpt());
    3434            }
    35            
     35
    3636            $t[] = get_the_excerpt();
    3737        }
     
    5454    }
    5555
    56     include __DIR__."/templates/options.php";   
     56    include __DIR__."/templates/options.php";
    5757}
    5858
     
    6868    // Register the options
    6969    add_option('acf_search_fields', serialize(array()));
    70    
    71     // Load the plugin data into an global 
    72     global $plugin_data;
    73     $plugin_data = get_plugin_data( __FILE__ );
     70
     71    // Load the plugin data into an global
     72    global $acf_search_plugin_data;
     73    $acf_search_plugin_data = get_plugin_data( __FILE__ );
    7474}
    7575
     
    7878 * @return [array] [list of custom fields]
    7979 */
    80 function list_searcheable_acf(){
    81   $list_searcheable_acf = (unserialize(get_option('acf_search_fields'))); //array("title", "sub_title", "excerpt_short", "excerpt_long", "sub_content", "content");
     80function acf_search_list_searcheable_acf(){
     81  $list_searcheable_acf = (unserialize(get_option('acf_search_fields')));
    8282  return $list_searcheable_acf;
    8383}
     
    9090 * credits to Vincent Zurczak for the base query structure/spliting tags section
    9191 */
    92 function advanced_custom_search( $where, &$wp_query ) {
     92function acf_search_advanced_custom_search( $where, &$wp_query ) {
    9393    global $wpdb;
    94        
     94
    9595    if ( empty( $where ))
    9696        return $where;
    97        
     97
    9898    // get search expression
    9999    $terms = $wp_query->query_vars[ 's' ];
    100    
     100
    101101    // explode search expression to get search terms
    102102    $exploded = explode( ' ', $terms );
    103103    if( $exploded === FALSE || count( $exploded ) == 0 )
    104104        $exploded = array( 0 => $terms );
    105          
     105
    106106    // reset search in order to rebuilt it as we whish
    107107    $where = '';
    108    
     108
    109109    // get searcheable_acf, a list of advanced custom fields you want to search content in
    110     $list_searcheable_acf = list_searcheable_acf();
    111        
     110    $list_searcheable_acf = acf_search_list_searcheable_acf();
     111
     112        // If you did not select any custom fields we need to return the default Query
     113        if(count($list_searcheable_acf) == 0) {
     114            return $where;
     115        }
     116
    112117    foreach( $exploded as $tag ) :
    113        
     118
    114119        $tag = esc_sql($wpdb->esc_like($tag));
    115        
    116         $where .= " 
     120
     121        $where .= "
    117122          AND (
    118123            (wp_posts.post_title LIKE '%{$tag}%')
     
    144149              WHERE (
    145150                taxonomy = 'post_tag'
    146                     OR taxonomy = 'category'               
     151                    OR taxonomy = 'category'
    147152                    OR taxonomy = 'myCustomTax'
    148153                )
     
    155160    return $where;
    156161}
    157  
    158 add_filter( 'posts_search', 'advanced_custom_search', 500, 2 );
     162
     163add_filter( 'posts_search', 'acf_search_advanced_custom_search', 500, 2 );
  • acf-search/trunk/readme.txt

    r1460094 r1460817  
    2929== Changelog ==
    3030
     31= 2.1.5 =
     32#2 When no CustomFields selected we return the default WP Search where-clause
     33
    3134= 2.1.4 =
    32 Small change regarding the “plugin_data” function
     35#1 Small change regarding the “plugin_data” function
    3336
    3437= 2.1.3 =
Note: See TracChangeset for help on using the changeset viewer.