This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
programmieren:wordpress:nach_cutom_fields_suchen [2022/08/17 15:36] – removed jgehrke | programmieren:wordpress:nach_cutom_fields_suchen [2022/12/17 12:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Wordpress: Suche mit Custom Fields oder ACF Feldern ====== | ||
+ | Dies ist ein Beispiel wie man in der Wordpress | ||
+ | |||
+ | Der Code Ursprung basiert auf diesem [[https:// | ||
+ | |||
+ | Zusätzlich ist noch ein Fix eingebaut, damit das Script auch Funktionen läuft, die WP Filter supressen. Lösung kam aus einem [[https:// | ||
+ | |||
+ | __**Hinweise zur Nutzung: | ||
+ | |||
+ | * **Manche AJAX-Aufrufe werden nicht als Suche interpretiert**. Dann bitte in der Methode '' | ||
+ | * **get_posts() unterdrückt Filter**. darum ist in der Methode '' | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | /* | ||
+ | HOOK FILTER IN SEARCH | ||
+ | */ | ||
+ | $post_types = [ ' | ||
+ | new SearchWithCustomFields( $post_types ); | ||
+ | |||
+ | |||
+ | |||
+ | /** | ||
+ | * | ||
+ | * === Code Ursprung | ||
+ | * Code basiert auf einem Beispiel von outsourcify.net | ||
+ | * @link: https:// | ||
+ | * | ||
+ | * === Original Bschreibung: | ||
+ | * Extend WordPress search to include custom fields | ||
+ | * @link: http:// | ||
+ | * | ||
+ | */ | ||
+ | class SearchWithCustomFields | ||
+ | { | ||
+ | |||
+ | private $wp_filter_priority | ||
+ | //private $acf_post_types | ||
+ | private $limit_search_to_post_types = []; // wp_query » post_types | ||
+ | private $results_per_page | ||
+ | |||
+ | |||
+ | function __construct( $post_types = [ ' | ||
+ | { | ||
+ | $this-> | ||
+ | |||
+ | add_filter( ' | ||
+ | add_filter( ' | ||
+ | add_filter( ' | ||
+ | add_filter( ' | ||
+ | add_filter( ' | ||
+ | } | ||
+ | |||
+ | public function set_post_types( $post_types = [] ) { $this-> | ||
+ | |||
+ | |||
+ | |||
+ | /** | ||
+ | * [manipulate_query_var description] | ||
+ | * | ||
+ | * @link https:// | ||
+ | * | ||
+ | * @param | ||
+ | * @return object|wp_query | ||
+ | */ | ||
+ | public function manipulate_wp_query( & | ||
+ | |||
+ | if ( $this-> | ||
+ | $wp_query-> | ||
+ | $wp_query-> | ||
+ | |||
+ | // wichtig, weil get_posts( $args ) Filter unterdrückt und es sonst nich geht | ||
+ | $wp_query-> | ||
+ | } | ||
+ | |||
+ | // Return query | ||
+ | return $wp_query; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | /** | ||
+ | * | ||
+ | * Für die Post-Metas einer temporären Tabelle hinzu, damit diese auch durchsuch werden | ||
+ | * | ||
+ | * @link: http:// | ||
+ | */ | ||
+ | public function sql_query_join_post_and_meta_on_search( $join_sql_query ) | ||
+ | { | ||
+ | global $wpdb; | ||
+ | |||
+ | if( $this-> | ||
+ | $join_sql_query .=' LEFT JOIN ' | ||
+ | } | ||
+ | |||
+ | return $join_sql_query; | ||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * | ||
+ | * Damit nicht nur im Title, sondern auch in einem Metafeld gesucht wird | ||
+ | * | ||
+ | * @link http:// | ||
+ | */ | ||
+ | public function sql_query_where_replace( $where_sql_query ) { | ||
+ | global $pagenow, $wpdb; | ||
+ | if ( $this-> | ||
+ | $where_sql_query = preg_replace( | ||
+ | "/ | ||
+ | " | ||
+ | } | ||
+ | return $where_sql_query; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | /** | ||
+ | * Duplikate verhindern | ||
+ | * | ||
+ | * @link http:// | ||
+ | */ | ||
+ | public function search_for_distinctive_results( $distinct_mode_string ) { | ||
+ | |||
+ | if ( $this-> | ||
+ | return " | ||
+ | } | ||
+ | return $distinct_mode_string; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | /** | ||
+ | * | ||
+ | * | ||
+ | * @link https:// | ||
+ | */ | ||
+ | public function filter_results( $post_objects_array ) { | ||
+ | |||
+ | return $post_objects_array ; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | /** | ||
+ | * Alias Methode von WP Funktion is_search() | ||
+ | * Ist hier eine eigene Methode, falls noch mehr Bedingen hinzu kommen. | ||
+ | * | ||
+ | * @link: https:// | ||
+ | * | ||
+ | * @return boolean | ||
+ | */ | ||
+ | private function is_search() | ||
+ | { | ||
+ | $is_search_by_wpquery | ||
+ | $is_admin | ||
+ | $is_acf_call | ||
+ | $is_flatsome_ajax_search = false; | ||
+ | |||
+ | /* Die Flatsome Theme Ajax Suche wird nicht als Suche erkannt, darum der Work Around */ | ||
+ | if( wp_doing_ajax() ){ | ||
+ | // $_REQUEST[' | ||
+ | // $_REQUEST[' | ||
+ | $is_flatsome_ajax_search = ( $_REQUEST[' | ||
+ | } | ||
+ | |||
+ | $is_search = ( ( $is_search_by_wpquery & !$is_admin & !$is_acf_call ) | $is_flatsome_ajax_search ) ? true : false; | ||
+ | |||
+ | 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 | ||
+ | $is_acf_call = false; | ||
+ | foreach ($backtrace as $key => $stack) { | ||
+ | $is_acf_file | ||
+ | $is_acf_function = (strpos( $stack[" | ||
+ | |||
+ | if( $is_acf_file | $is_acf_function ){ | ||
+ | $is_acf_call = true; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return $is_acf_call; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ |