From 7639ba16e4c38cf01916f1ade57657f52d64eafa Mon Sep 17 00:00:00 2001 From: jorge-vitrubio Date: Sat, 7 Jan 2023 13:32:33 +0100 Subject: [PATCH] added suport for search in plugin rather than in theme --- includes/custom-field-ajuts-filter.php | 72 ++++++++++++++++++++++++++ includes/custom-post-type-ajuts.php | 15 +++--- ofisuport-wp-plugin.php | 10 ++-- 3 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 includes/custom-field-ajuts-filter.php diff --git a/includes/custom-field-ajuts-filter.php b/includes/custom-field-ajuts-filter.php new file mode 100644 index 0000000..db775e1 --- /dev/null +++ b/includes/custom-field-ajuts-filter.php @@ -0,0 +1,72 @@ + field name) +$GLOBALS['my_query_filters'] = array( + 'fundtarget' => 'os_fund_target', + 'fundsource' => 'os_fund_source', + 'fundfield' => 'os_fund_field' +); +$GLOBALS['my_query_filters_dates'] = array( + 'fundapplybegin' => 'os_fund_apply_begin', + 'fundapplyend' => 'os_fund_apply_end' + //'fundcall' => 'os_fund_call', +); + +// action +add_action('pre_get_posts', 'my_pre_get_posts',10,1); + +function my_pre_get_posts( $query ) { + + // bail early if is in admin + if( is_admin() ) { return; } + + // bail early if not main query or plugin $the_query_ajut + // - allows custom code / plugins to continue working + if( !$query->is_main_query() ) return; + + + + // loop over filters + foreach( $GLOBALS['my_query_filters'] as $key => $fieldname) + { + // continue if not found in url + if( empty($_GET[ $fieldname ]) ) { continue; } + + // get original meta query + $meta_query = []; + $meta_query[] = $query->get('meta_query'); + $meta_query[] = array('relation' => 'OR'); + + // get the values for this filter + // eg: http://domain.tdl/ajut/?os_fund_target=autonoma,entitat-publica + $filtervalues= explode(',', $_GET[ $fieldname ]); + + // loop retreived values from checkboxes and filter them with REGEXP + // http://domain.tdl/ajut/?os_fund_target=autonoma&os_fund_field=circi + // http://domain.tdl/ajut/?os_fund_target=autonoma,entitat-publica&os_fund_field=dansa + foreach( $filtervalues as $filteredvalue ) + { + // they are array based fields + // https://barn2.com/blog/querying-posts-by-custom-field-acf/#array-based-fields + $strippedvalue = sprintf( '^%1$s$|s:%2$u:"%1$s";', $filteredvalue, strlen( $filteredvalue ) ); + + // add our meta query to the original meta queries + $meta_query[] = array( + array( + 'key' => $fieldname, + 'value' => $strippedvalue, + 'compare' => 'REGEXP', + ), + ); + } + // update the meta query arguments + $query->set('meta_query', $meta_query); + } + + //always return + return; +} diff --git a/includes/custom-post-type-ajuts.php b/includes/custom-post-type-ajuts.php index b308b15..e07dc90 100644 --- a/includes/custom-post-type-ajuts.php +++ b/includes/custom-post-type-ajuts.php @@ -40,7 +40,7 @@ 'not_ajut_in_trash' => __( 'No hem trovat cap ajut a la brosa', 'text_domain' ) ); $rewrite = array( - 'slug' => 'ajut' + 'slug' => 'ajuts' ); // https://wordpress.stackexchange.com/questions/108338/capabilities-and-custom-post-types#108375 // https://developer.wordpress.org/reference/functions/register_post_type/#capability_type @@ -59,11 +59,12 @@ 'label' => __( 'ofisuport-ajut', 'text_domain' ), 'description' => __( 'Ajuts', 'text_domain' ), 'labels' => $labels, - 'supports' => array( 'title', 'editor', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes' ), - 'taxonomies' => array( - 'ofisuport-ajut-category', - 'ofisuport-ajut-tag' - ), + //'supports' => array( 'title', 'editor', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes' ), + 'supports' => array( 'title', 'editor', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes' ), + // 'taxonomies' => array( + // 'ofisuport-ajut-category', + // 'ofisuport-ajut-tag' + // ), 'hierarchical' => true, 'public' => true, 'show_ui' => true, @@ -80,7 +81,7 @@ 'capability_type' => 'post', 'rewrite' => $rewrite, //'capabilities' => $capabilities, - 'map_meta_cap' => true //neded to apply the new capabilities. + //'map_meta_cap' => true //neded to apply the new capabilities. ); register_post_type( 'ofisuport-ajut', $args ); } diff --git a/ofisuport-wp-plugin.php b/ofisuport-wp-plugin.php index b794a59..4e1b955 100644 --- a/ofisuport-wp-plugin.php +++ b/ofisuport-wp-plugin.php @@ -59,11 +59,13 @@ include( OFISUPORT_WPPLUGIN_PATH . 'includes/plugin-init-textdomain.php'); include( OFISUPORT_WPPLUGIN_PATH . 'includes/enable-svg.php'); -//if ( is_admin() ) { - include( OFISUPORT_WPPLUGIN_PATH . 'includes/stylesheet-admin.php'); +if ( is_admin() ) { +include( OFISUPORT_WPPLUGIN_PATH . 'includes/stylesheet-admin.php'); //} else { - include( OFISUPORT_WPPLUGIN_PATH . 'includes/stylesheet-public.php'); -//} +//include( OFISUPORT_WPPLUGIN_PATH . 'includes/stylesheet-public.php'); +} include( OFISUPORT_WPPLUGIN_PATH . 'includes/custom-post-type-ajuts.php'); +include( OFISUPORT_WPPLUGIN_PATH . 'includes/custom-field-ajuts-filter.php'); +