How would I filter posts on page based on a custom field selected by user

Hi all, relatively beginner dev here. I’m taking a crack at filtering the posts on the archive pages on my site for a few custom post types. For the record, my site is built using Blue Theme. These post types have a custom field (created by ACF) to specify a region. Currently, each archive page displays all the posts of that custom type using this code:

 

<div class=”posts_group lm_wrapper col-12 <?php echo esc_attr(implode(‘ ‘, $blog_classes)); ?>”>
<?php

$attr = array(
‘echo’ => true,
‘featured_image’ => false,
‘filters’ => $filters,
);

if ($load_more) {
$attr[‘featured_image’] = ‘no_slider’;
}
if (mfn_opts_get(‘blog-images’)) {
$attr[‘featured_image’] = ‘image’;
}

echo mfn_content_post(false, false, $attr);
?>
</div>

<?php
if ( mfn_opts_get( ‘blog-infinite-scroll’ ) ):

echo ‘<div class=”mfn-infinite-load-button”>’. mfn_pagination( false, mfn_opts_get(‘blog-posts’) ) .'</div>’;

elseif (function_exists(‘mfn_pagination’)):

echo mfn_pagination(false, $load_more);

else:
?>
<div class=”nav-next”><?php next_posts_link(__(‘&larr; Older Entries’, ‘betheme’)) ?></div>
<div class=”nav-previous”><?php previous_posts_link(__(‘Newer Entries &rarr;’, ‘betheme’)) ?></div>
<?php
endif;
?>
&nbsp;

I found [this guide](https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/) online and tried to integrate the functionality into my website, but I can’t seem to get any sort of response by adding an argument to the end of my URL like the user in the video is. I think it might have to do with the fact that although I’m using the archive template for the page I’m viewing, simply adding the argument doesn’t work properly.

I modified the code used in the video to fit the field name I created, but there might be an important defining step I’m missing:

&nbsp;

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;
}

// get meta query
$meta_query = $query->get(‘meta_query’);

// allow the url to be altered by filter selected
if( isset($_GET[‘region’]) )
{
$region = explode(‘,’, $_GET[‘region’]);

// append meta query to original meta queries
$meta_query[] = array(
‘key’ => ‘region’,
‘value’ => $region,
‘compare’ => ‘IN’,
);

}

// update meta query
$query->set(‘meta_query’, $meta_query);
return;
}

&nbsp;

Where am I going wrong? Before I can set up a method of having user select a location from a popup, I first need to get the filtering system working.

Any help would be much appreciated. Thanks all.

2 Comments
  1. Possibly because you explode $region into an array, but then just use it as a string in the key.

    Either go $regions = explode

    then $region = $regions[0] to extract the first item

    or use key => ‘$region[0]’

    Or even

    foreach $regions as $reg

    I find wp can be fussy about whether it lets you reference an array directly – depending on if your in/out of the loop, or what part of the chain it’s at… but twiddle with getting your array into a single value – or declaring ‘key’ as an array of values.

  2. You definitely have a ACF field in your custom post type called ‘region’??
    Secondly id test to see if the $region array actually has anything in it, so print_r($region) inside the filter and see if it kicks anything out at the top of the page, if it doesn’t you have problems there.

    There is an alternative method of using get_query_vars, which might work better.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Your Advertisement Here