added filter for ajut fund with checkboxes

This commit is contained in:
jorge 2023-01-06 13:20:49 +01:00
parent f11cfa6de2
commit ad5777f611
12 changed files with 253 additions and 81 deletions

View File

@ -1385,34 +1385,34 @@ Add your custom styles in this file so it is easier to update the theme.
text-decoration: none;
font-size: 0.8rem;
}
.edit-post-visual-editor__content-area .page-template-funds-search .ofisuport-filtered-funds {
.edit-post-visual-editor__content-area .ofisuport-filtered-funds {
display: grid;
grid-template-columns: 1fr 1fr;
}
.edit-post-visual-editor__content-area .page-template-funds-search .ofisuport-fund-filter {
.edit-post-visual-editor__content-area .ofisuport-fund-filter {
display: grid;
grid-template-columns: repeat(1, 1fr);
gap: 1rem;
}
.edit-post-visual-editor__content-area .page-template-funds-search .archive-posts {
.edit-post-visual-editor__content-area .archive-posts {
grid-template-columns: repeat(1, 1fr);
}
}
@media screen and (min-width: 48em) {
.edit-post-visual-editor__content-area .page-template-funds-search .ofisuport-filtered-funds {
.edit-post-visual-editor__content-area .ofisuport-filtered-funds {
grid-template-columns: 1fr 2fr;
}
.edit-post-visual-editor__content-area .page-template-funds-search .archive-posts {
.edit-post-visual-editor__content-area .archive-posts {
grid-template-columns: repeat(2, 1fr);
}
}
@media screen and (min-width: 80em) {
.edit-post-visual-editor__content-area .page-template-funds-search .ofisuport-filtered-funds {
.edit-post-visual-editor__content-area .ofisuport-filtered-funds {
grid-template-columns: 1fr 3fr;
}
.edit-post-visual-editor__content-area .page-template-funds-search .archive-posts {
.edit-post-visual-editor__content-area .archive-posts {
grid-template-columns: repeat(3, 1fr);
}
}

File diff suppressed because one or more lines are too long

106
archive-ofisuport-ajut.php Normal file
View File

@ -0,0 +1,106 @@
<?php
/**
* The template for displaying archive custom post type ajut
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package Oficina_de_Suport_theme
*/
get_header();
?>
<main id="primary" class="site-main">
<section id="filteredfunds" class="ofisuport-filtered-funds ofisuport-ajuts-filtrats">
<aside id="fundsfilter" class="ofisuport-filter-funds ofisuport-fitre-ajuts">
<div id="arhive-filters" class="ofisuport-filters">
<?php
// output all possible values of a checkbox
$groupkey = "group_63ab636898703"; // write here the key for the group of fields from acf
if( $groupkey){
//echo '<h6>check the groupkey <span style="color: #00b">' . $groupkey . '</span></h6>';
$groupkey_fields = acf_get_fields($groupkey);
foreach( $groupkey_fields as $field_key ) {
if( $field_key['type'] == 'checkbox' ){
//echo '<span style="color: #00b">' . ($field_key['key']) . '</span><br/>';
//echo '<span style="color: #00b">' . ($field_key['label']) . '</span><br/>';
//echo '<span style="color: #00b">' . ($field_key['name']) . '</span><br/>';
// https://wordpress.stackexchange.com/a/102915
//$field_key = "field_XXXXXX";// the acf field key unic numbers
$field_key = $field_key['key'];
$fields = get_field_object($field_key);
if( $fields )
{
// check for values in url
foreach( $GLOBALS['my_query_filters'] as $key => $fieldname ):
// set value if available
if( isset($_GET[ $fieldname ]) ) {
$filteredvalues['value'] = explode(',', $_GET[ $fieldname ]);
};
// construct the checkboxes
if( $fieldname == $fields['name']) {
echo '<nav id="fund-filter" class="ofisuport-filter ' . $fields['name'] . '">' ;
echo '<h5>' . $fields['label'] . '</h5>';
echo '<ul>';
foreach( $fields['choices'] as $choicevalue => $choicelabel ) {
echo '<li>';
echo '<input type="checkbox" ';
echo ' value="' . $choicevalue . '" ';
if( in_array($choicevalue,$filteredvalues['value']) ):
echo ' checked="checked" ';
endif;
echo ' />';
echo $choicelabel;
echo '</li>';
}
echo '</ul>';
echo '</nav>';
};
// end of construct checkboxes
endforeach;
// end check for values in url
}
};//end if type checkbox
};//end foreach groupkey field
};//end if groupkey
?>
<?php //endforeach; ?>
</div>
</aside>
<content class="archive-posts <?php ofisuport_class_posttype(); ?>">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post();
get_template_part( 'template-parts/section', 'eachpost' );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
</content>
</section>
</main><!-- #main -->
<?php
get_footer();

File diff suppressed because one or more lines are too long

View File

@ -181,3 +181,9 @@ require get_template_directory() . '/inc/archive-title.php';
* Functions to add admin styles
*/
require get_template_directory() . '/inc/admin-style.php';
/**
* Function to add filter in ofisuport-ajut custom post
*/
// moved to plugin on 2023 01 05
//require get_template_directory() . '/inc/ofisuport-ajut-filter.php';

View File

@ -0,0 +1,42 @@
<?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['os_fund_target']) )
{
// get original meta query
$meta_query = [];
$meta_query[] = array('relation' => 'OR');
$meta_query[] = $query->get('meta_query');
$fieldsearchvalues= explode(',', $_GET['os_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' => 'os_fund_target',
'value' => $os_field_value,
'compare' => 'REGEXP',
),
);
}
// update the meta query arguments
$query->set('meta_query', $meta_query);
}
//always return
return;
}

View File

@ -21,15 +21,9 @@ get_header();
<header class="page-header">
</header><!-- .page-header -->
<?php
/* Start the Loop */
while ( have_posts() ) :
the_post();
/*
* Include the Post-Type-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Type name) and that will be used instead.
*/
get_template_part( 'template-parts/content', 'page' );
endwhile;
@ -42,33 +36,50 @@ get_header();
endif;
?>
<section id="filteredfunds" class="ofisuport-filtered-funds ofisuport-ajuts-filtrats">
<aside id="fundsfilter" class="ofisuport-filter-funds ofisuport-fitre-ajuts">
<?php include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); if ( is_plugin_active( 'oficinasuport-wp-plugin/ofisuport-wp-plugin.php' ) ) { // if plugin active do?>
<nav class="sidebar fund-filter">
<h5>filter options</h5>
<ul>
<li>item 01</li>
<li>item 02</li>
<li>item 03</li>
<li>item 04</li>
</ul>
</nav>
<?php } //end if is_plugin_active ?>
</aside>
<content class="archive-posts <?php ofisuport_class_posttype(); ?>">
<?php
// https://support.advancedcustomfields.com/forums/topic/wp_query-using-meta_query-for-an-acf-checkbox-field/#post-145830
// https://www.advancedcustomfields.com/resources/checkbox/#query-posts
// https://barn2.com/blog/querying-posts-by-custom-field-acfi/
$the_query_ajut = new WP_Query(
array(
'post_type' => 'ofisuport-ajut',
'post_per_page' => '6',
) );
'post_per_page' => '6'
)
);
?>
<?php if ( $the_query_ajut->have_posts() ) : ?>
<?php while ( $the_query_ajut->have_posts() ) : $the_query_ajut->the_post(); ?>
<?php get_template_part( 'template-parts/section', 'eachpost' ); ?>
<?php
//the_title('<h3>','</h3>');
// echo '<ul>';
//$postmetas = get_post_meta(get_the_ID());
// foreach($postmetas as $meta_key=>$meta_value) {
// echo '<li><code>' . $meta_key . '</code>:<code>' . $meta_value[0] . '</code></li>';
// } ;
// echo '</ul>';
?>
<?php endwhile;//end of the loop ?>
<?php wp_reset_postdata(); ?>
<?php endif; //end query ajut ?>

View File

@ -78,7 +78,7 @@
font-size: 0.8rem;
}
}
.page-template-funds-search {
// .page-template-funds-search {
.ofisuport-filtered-funds {
display:grid;
grid-template-columns: 1fr 1fr;
@ -91,7 +91,7 @@
.archive-posts {
grid-template-columns: repeat(1 , 1fr);
}
}
//}
}
@media screen and (max-width: 48em) {
@ -99,7 +99,7 @@
}
@media screen and (min-width: 48em) {
//only bigger than 48em
.page-template-funds-search {
//.page-template-funds-search {
.ofisuport-filtered-funds {
grid-template-columns: 1fr 2fr;
}
@ -108,11 +108,11 @@
.archive-posts {
grid-template-columns: repeat(2 , 1fr);
}
}
//}
}
@media screen and (min-width: 80em) {
//only bigger than 80rem
.page-template-funds-search {
//.page-template-funds-search {
.ofisuport-filtered-funds {
grid-template-columns: 1fr 3fr;
}
@ -122,5 +122,5 @@
grid-template-columns: repeat(3 , 1fr);
}
}
//}
}

View File

@ -188,6 +188,11 @@
.main-navigation.toggled ul {
display: block;
}
.ofisuport-filters {
.ofisuport-filter {
//float: left;
}
}
}
@media screen and (min-width: 48em) {
.site-branding {

View File

@ -1327,34 +1327,34 @@ textarea {
text-decoration: none;
font-size: 0.8rem;
}
.page-template-funds-search .ofisuport-filtered-funds {
.ofisuport-filtered-funds {
display: grid;
grid-template-columns: 1fr 1fr;
}
.page-template-funds-search .ofisuport-fund-filter {
.ofisuport-fund-filter {
display: grid;
grid-template-columns: repeat(1, 1fr);
gap: 1rem;
}
.page-template-funds-search .archive-posts {
.archive-posts {
grid-template-columns: repeat(1, 1fr);
}
}
@media screen and (min-width: 48em) {
.page-template-funds-search .ofisuport-filtered-funds {
.ofisuport-filtered-funds {
grid-template-columns: 1fr 2fr;
}
.page-template-funds-search .archive-posts {
.archive-posts {
grid-template-columns: repeat(2, 1fr);
}
}
@media screen and (min-width: 80em) {
.page-template-funds-search .ofisuport-filtered-funds {
.ofisuport-filtered-funds {
grid-template-columns: 1fr 3fr;
}
.page-template-funds-search .archive-posts {
.archive-posts {
grid-template-columns: repeat(3, 1fr);
}
}

File diff suppressed because one or more lines are too long

View File

@ -17,12 +17,14 @@
?>
<?php
$values= get_field($fielditerate);
if( $values ):
$fields= get_field($fielditerate);
if( $fields ):
?>
<ul class="oficina-suport-label item-<?php echo $fielditerate ?>">
<?php foreach( $values as $value ): ?>
<li><?php echo $value; ?></li>
<?php foreach( $fields as $field ): ?>
<li>
<?php echo $field['label']; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>