xarxaprod-wp-theme/inc/xarxaprod-ajut-filter.php

43 lines
1.2 KiB
PHP

<?php
/**
* Makes a filter for the custom post type ajut and its custom fields
*/
// action
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ) { return; }
// allow the url to alter the query
if( isset($_GET['xxp_fund_target']) )
{
// get original meta query
$meta_query = [];
$meta_query[] = array('relation' => 'OR');
$meta_query[] = $query->get('meta_query');
$fieldsearchvalues= explode(',', $_GET['xxp_fund_target']);
foreach( $fieldsearchvalues as $fieldsearchvalue ){
$os_search_value = $fieldsearchvalue; //'entitat-publica'; // whatever
$os_field_value = sprintf( '^%1$s$|s:%2$u:"%1$s";', $os_search_value, strlen( $os_search_value ) );
// add aur meta query to the original meta queries
$meta_query[] = array(
array(
'key' => 'xxp_fund_target',
'value' => $os_field_value,
'compare' => 'REGEXP',
),
);
}
// update the meta query arguments
$query->set('meta_query', $meta_query);
}
//always return
return;
}