I have created a twig template to compare some user and post taxonomies that outputs the correct custom post type id. This works with ACF and twig template fine and outputs correctly. Now I thought it would be simple to just hook into a string to use these ids for a looper e.g. p=1 will output hello word default post but testing even without using the twig template putting in the post id for my acf custom post doesn’t output anything. So thought I would ask here the best approach for this.
Incase helpful maybe for others doing similar this is current code I have for comparing making use of twig.
// Register the custom filter for getting post IDs (modified)
add_filter('cs_twig_filters', function($filters) {
$filters['get_matching_leads'] = [
'callable' => function($user_id) {
// If no user is logged in, return an empty array
if (!$user_id) {
return [];
}
// Get the 'job_types' and 'location' taxonomies from user fields
$job_types = get_field('job_types', 'user_' . $user_id); // 'roofing-type' taxonomy
$location = get_field('location', 'user_' . $user_id); // 'region' taxonomy
// Ensure the fields are arrays (handling both single and multiple terms)
$job_types = is_array($job_types) ? wp_list_pluck($job_types, 'slug') : [$job_types];
$location = is_array($location) ? wp_list_pluck($location, 'slug') : [$location];
// Set up the query arguments for fetching posts
$args = [
'post_type' => 'lead', // Custom post type 'lead'
'posts_per_page' => -1, // Retrieve all matching posts
'fields' => 'ids', // Only fetch post IDs for better performance
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'roofing-type',
'field' => 'slug',
'terms' => $job_types, // Match against 'roofing-type' taxonomy
],
[
'taxonomy' => 'region',
'field' => 'slug',
'terms' => $location, // Match against 'region' taxonomy
],
],
];
// Execute the query and return the post IDs
$posts = get_posts($args);
return $posts; // Return array of post IDs
}
];
return $filters;
});
Which this is the twig template that outputs the ids in nice comma seperated format. Though as mentioned I can’t get the string to work with direct post id right now which I thought would just work like default posts.
{% if user %}
<h2>Matching Leads for User</h2>
<p>
<!-- Use the custom filter to fetch matching lead IDs based on user taxonomies -->
{% set matching_leads = user.id | get_matching_leads %}
{% if matching_leads is not empty %}
<!-- Join the matching lead IDs with commas, no space after commas -->
{{ matching_leads | join(',') }}
{% else %}
<span>No matching leads found</span>
{% endif %}
</p>
{% else %}
<p>No user is logged in.</p>
{% endif %}