Home › Forums › Backend Issues (wp-admin) › How can I filter a posts relationship field by the current category edit screen?
Soooo I’m trying to:
1) add a per-category relationship field to the edit screen for each category
2) filter the posts returned in the relationship field on each category edit screen by the id of the category currently being edited. In other words, if I’m editing a given category’s edit screen, I want a relationship field containing only posts from that category.
I’ve got part 1 working fine. Easy enough.
Part two, however — I’m stumped.
Here’s what I’ve got so far:
function limit_cat_post_relationships( $args, $field, $post )
{
$args['category__in'] = $_GET['tag_ID'];
return $args;
}
add_filter('acf/fields/relationship/query/name=ibm_topic_feat_posts', 'limit_cat_post_relationships', 10, 3);
If I feed an explicit numerical cat id value to the argument $args['category__in'] = 44
, for example, to test, it works — but I’m struggling to get that to happen dynamically based on the currently-being-edited category. $_GET['tag_ID'];
doesn’t work, and I’m not sure how else to retrieve the id of the current edit screen from within the filter function.
Any help figuring this out would be truly appreciated!!!!!
I think (but it’s only a guess) that the problem is $_GET[‘tag_ID’] is a string value and WP might require this to be a number. Try
$args['category__in'] = intval($_GET['tag_ID']);
Needed this today, and found the solution.
Here it is in case someone needs it:
function limit_cat_post_relationships( $title, $post3, $field, $post_id )
{
/* Because this is proccesd as an AJAX request
* we can get the referer, and extract the tag_ID from there
*/
$url = wp_get_referer();
$parts = parse_url($url);
parse_str($parts['query'], $query);
$title['tax_query'][0]['terms'][0] = $query['tag_ID'];
return $title;
}
add_filter('acf/fields/relationship/query/name=ibm_topic_feat_posts', 'limit_cat_post_relationships', 10, 3);
I had the same problem, wanted to limit current term in custom taxonomy in wp-admin.
Here is my solution.
// Show 2000 posts pre ajax call, change sort, limit to current taxonomy and term id
add_filter('acf/fields/relationship/query/name=author_sort_list', 'authors_fields_relationship_query', 10, 3);
function authors_fields_relationship_query( $args, $field, $post_id ) {
$url = wp_get_referer();
$parts = parse_url($url);
parse_str($parts['query'], $query);
$term_id = $query['tag_ID'];
$taxonomy = $query['taxonomy'];
if ( isset($taxonomy) && isset($term_id)) {
$args['tax_query'] = array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term_id,
'operator' => 'IN'
)
);
}
$args['posts_per_page'] = 2000;
$args['orderby'] = 'date';
$args['order'] = 'ASC';
return $args;
}
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’re hard at work on ACF 6.1, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.