Looper for outputting specific posts

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

Hello Jay,

Thank you for the inquiry.

Have you tried using the cs_twig_functions filter instead?

// Register the custom function for getting post IDs using cs_twig_functions
add_filter('cs_twig_functions', function($functions) {
    $functions['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 only post IDs as an array
        }
    ];

    return $functions;
});

You can then use this function in a Twig template, like this:

{% set matching_leads  = get_matching_leads(user.id) %}

Let us know if this works for you.

Best regards,
Ismael

Hi Ismeal thanks for fast response, so the twig part actually is working correctly it’s just how do I add this to a looper provider now. I thought I could do a query string would be simple way but that doesn’t actually seem to work for acf custom post type e.g. p=1 will pull hello world post and p=425 the id of test post pulls correct but and comma separated p=1,425 will pull both. But if I put the id 407 it seems to break and just pull the page viewing instead of custom post type lead. So wondering if some setting rather than code I am missing. Once that works manually I should be able to just replace with the twig template just currently manually testing found not working. The twig template was more just sharing code for anyone interested in doing similar as I know it is useful.

Thank you for the update.

It might be a little unclear what you are trying to do here. Where are you testing this? Please create a staging site or a test page, then provide the login details in the secure note so that we can check the modification further.

We don’t have the “lead” custom post type on our end, but when we test the following function for default posts, it successfully retrieves the items.

// Register the custom function for getting all post IDs
add_filter('cs_twig_functions', function($functions) {
    $functions['get_all_post_ids'] = [
        'callable' => function() {
           
           $args = [
                'post_type' => 'post',  
                'posts_per_page' => -1, 
                'fields' => 'ids'       
            ];

            $posts = get_posts($args);
            return $posts;  
        }
    ];

    return $functions;
});

In a Twig template:

{% set posts = get_all_post_ids() %}

No worries got it working it was about getting the twig template for the custom term sort to work with loop provider ended up going the route custom loop provider with the below function

    add_filter( 'cs_looper_custom_my_leads', function( $result, $params ) {

    if ( ! isset( $params['post_ids'] ) || empty( $params['post_ids'] ) ) {
        return [];
    }


    $post_ids = array_map( 'absint', explode( ',', $params['post_ids'] ) );
	
    $paged = isset( $params['paged'] ) ? absint( $params['paged'] ) : 1; // Default to page 1
    $posts_per_page = isset( $params['posts_per_page'] ) ? absint( $params['posts_per_page'] ) : 10; // Default to 10 posts per page
	
	
    $args = array(
        'post_type' => 'lead', 
        'post__in'  => $post_ids, 
        'orderby'   => 'post__in',
        'paged'          => $paged,
        'posts_per_page' => $posts_per_page,
    );

    $query = new WP_Query( $args );
	
	$GLOBALS['my_leads_pagination'] = array(
        'total_pages' => $query->max_num_pages,
        'current_page' => $paged,
    );
	
    return $query->posts;
	
}, 10, 2 );

Then used the hook my_leads with parameter below that includes the twig for comparing user and lead post taxamonies. Thought post my full solutions here so anyone else looking to do similar displaying specific posts that match a post and user taxaomonies for a more advanced custom user dashboard built and all designed in cornerstone.

{ "post_ids": "{% include 'cs-template:displayleads' %}", "paged": "1", "posts_per_page" : "1"}

Hey Jay,

We’re glad that you’re able to figure it out!

Cheers!