Home › Forums › Front-end Issues › Archive Page, Filter by Custom Fields inside ACF Relationship
I have two custom post types: Stores and Products. When adding a new Stores post, there is an ACF Relationship field to select the Products that would be available at that single store.
Is it possible on the archive page for Stores, to filter by a custom field that is inside the Product post type? For example, a custom field on the Product post type is ‘Status’ which is a radio selection of available, unavailable and archived. Currently, I can only filter the Stores by the custom fields directly associated with the Stores post type.
I’m looking for something like: domain.com/stores/?status=available or domain.com/stores/?status=archived.
Tutorial I’m referencing: https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/
archive-stores.php
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$products = get_field('products'); // acf relationship
?>
<div class="store">
<h4><?php echo get_the_title(); ?></h4>
<?php if( $products ): ?>
<?php foreach( $products as $product ):
$status = get_field('status', $product->ID);
$type = get_field('type_tax', $product->ID);
?>
[url=]<?php echo get_the_title( $product->ID ); ?>
<div>Status: <?php echo $status; ?></div>
<div>Type: <?php echo $type; ?></div>
<br>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
functions.php
<?php
$GLOBALS['my_query_filters'] = array(
'field_5f786da6ec5a6' => 'status',
);
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
function my_pre_get_posts( $query ) {
if( is_admin() ) return;
if( !$query->is_main_query() ) return;
$meta_query = $query->get('meta_query');
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
if( empty($_GET[ $name ]) ) {
continue;
}
$value = explode(',', $_GET[ $name ]);
$meta_query = array(
array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
)
);
}
$query->set('meta_query', $meta_query);
return;
}
?>
—- Versions —-
ACF v5.8.9
WP v5.4.2
Currently, I can only filter the Stores by the custom fields directly associated with the Stores post type.
This is always. In order to filter the store by the field of a product that field would also need to be stored associated with the store.
however… what I’m seeing is that you want to filter the products while showing the store and not the stores…..
// get the product ID values
$product_ids = get_field('products', false, false); // acf relationship
// by setting the 3rd parameter to false you get just a list of post IDs
if ($product_ids) {
// ensure that products is an array,
// and that the values are integers
if (!is_array($product_ids)) {
$product_ids = array($product_ids);
}
$product_ids = array_map('intval', $product_ids);
// do your own query to get the posts
// I only include the necessities here
$args = array(
'post_type' => 'product',
'post__in' => $product_ids,
'meta_query' => array(
array(
'key' => 'status'
'value' => $_GET['product_status']
)
)
);
$products = new WP_Query($args);
if ($products->have_posts()) {
while ($products->have_posts()) {
$products->the_post();
// output product data here
}
wp_reset_postdata();
}
} // end if products
Of course you’d also need to adjust the store meta query to skip the fields associated with the product.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.