Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programmieren:wordpress:nach_cutom_fields_suchen [2022/08/17 16:19] jgehrkeprogrammieren:wordpress:nach_cutom_fields_suchen [2022/12/17 12:28] (current) – external edit 127.0.0.1
Line 14: Line 14:
 <code php> <code php>
 <?php <?php
 +
 /* /*
  HOOK FILTER IN SEARCH  HOOK FILTER IN SEARCH
  */  */
- 
-// diese Zeilen müssen nur 1x früh ausgeführt werden und dann geht die ACF Feld Suche bei jeder Wordpress-Suche 
 $post_types = [ 'post', 'page', 'dv_betriebe' ]; $post_types = [ 'post', 'page', 'dv_betriebe' ];
 new SearchWithCustomFields( $post_types ); new SearchWithCustomFields( $post_types );
Line 39: Line 38:
  
  private $wp_filter_priority         = 100;  private $wp_filter_priority         = 100;
 + //private $acf_post_types             = [ 'acf-field-group', 'acf-field' ]; // ungenuzt
  private $limit_search_to_post_types = []; // wp_query » post_types  private $limit_search_to_post_types = []; // wp_query » post_types
  private $results_per_page           = -1; // wp_query » posts_per_page  private $results_per_page           = -1; // wp_query » posts_per_page
Line 51: Line 51:
  add_filter( 'posts_where',    [ $this, 'sql_query_where_replace' ],                $this->wp_filter_priority );  add_filter( 'posts_where',    [ $this, 'sql_query_where_replace' ],                $this->wp_filter_priority );
  add_filter( 'posts_distinct', [ $this, 'search_for_distinctive_results' ],         $this->wp_filter_priority );  add_filter( 'posts_distinct', [ $this, 'search_for_distinctive_results' ],         $this->wp_filter_priority );
- //add_filter( 'posts_results',  [ $this, '' ], $this->wp_filter_priority );+ add_filter( 'posts_results',  [ $this, 'filter_results' ], $this->wp_filter_priority );
  }  }
  
Line 129: Line 129:
      }      }
      return $distinct_mode_string;      return $distinct_mode_string;
 + }
 +
 +
 +
 + /**
 + *
 + *
 + * @link https://developer.wordpress.org/reference/hooks/posts_results/
 + */
 + public function filter_results( $post_objects_array ) {
 +
 +     return $post_objects_array ;
  }  }
  
Line 144: Line 156:
  {  {
  $is_search_by_wpquery    = is_search();  $is_search_by_wpquery    = is_search();
 + $is_admin                = is_admin();
 + $is_acf_call             = $this->is_acf_call();
  $is_flatsome_ajax_search = false;  $is_flatsome_ajax_search = false;
  
Line 153: Line 167:
  }  }
  
- $is_search = ( $is_search_by_wpquery | $is_flatsome_ajax_search ) ? true : false;+ $is_search = ( $is_search_by_wpquery & !$is_admin & !$is_acf_call ) | $is_flatsome_ajax_search ) ? true : false;
  
  return $is_search;  return $is_search;
 + }
 +
 + /**
 + *
 + *
 + * Prüft ob die Suche durch ein ACF Query ausgelöst wurde
 + *
 + * ACF & die Beschränkung nach Post_Types vertägt sich nicht, weil
 + * ACF mit eigenen Posttypes kommt. Darum muss geprüft werden, dass
 + * keine Beschränkung in der Suche vorgenommen wird, wenn der call
 + * über ACF kommt.
 + *
 + * Bisher noch keine bessere Lösung gefunden, als den Callstack
 + * abzufragen.
 + *
 + * @return boolean [description]
 + */
 + private function is_acf_call()
 + {
 + $backtrace   = debug_backtrace();
 + $is_acf_call = false;
 + foreach ($backtrace as $key => $stack) {
 + $is_acf_file     = (strpos( $stack["file"], 'advanced-custom-fields-pro' ) !== 0 ) ? true : false;
 + $is_acf_function = (strpos( $stack["function"], 'get_field' ) !== 0 ) ? true : false;
 +
 + if( $is_acf_file | $is_acf_function ){
 + $is_acf_call = true;
 + break;
 + }
 + }
 +
 + return $is_acf_call;
  }  }
 } }
 +
 </code> </code>

Page Tools