Home Forums Front-end Issues Output CPT posts based on dynamically matching ACF field value in 2nd CPT


Output CPT posts based on dynamically matching ACF field value in 2nd CPT

  • Hi,

    as this is my first forum thread here, I hope to be in line with the forum rules.
    I love ACF and can find my way around, but now I have to tackle something, which is realy challenging for me.

    I have a post-tpye “lifestyle_data” where I have 50 ACF fields.
    A user fills out a form and on form-submit a lifetyle-data entry with the options for those 50 ACF fields is created with the user being the author.

    Next I have a post-type “analysis_textblock”, where I have the regular WP content plus the same field-group with those 50 ACF fields.

    We setup about 150 or more analysis_textblock posts with some content and select a value for one of those ACF fields.

    We want to output matching analysis_textblocks content on a “recommendation” page.

    What I want to come up with:

    • Get the lifestyle-data post from the current user
    • go through all ACF fields (dynamically) and the their values (single values and arrays)

    For the next part I am not sure, what is the appropriate solution, especially thinking about performance.

    We would like to get all “analysis_textblock” posts (more than 150 posts), get the ACF fields for each (50 ACF fields for each textblock, even though only one or two will have selected values) and find the ones, where the field value equals the value of the same field in the users lifestyle_data post.

    We will output the “recommendations” (all matching analysis_textblock posts) as a shortcode.

    Here is what I have so far

    function related_textblocks( $user=false ) {
        // Get current user
            $user = wp_get_current_user();
        if ( 0 == $user->ID ) {
            // Not logged in.
        // Get lifestyle-data post from current user
        $user_lifestyle_data = array(
            'numberposts' => '1',
            'author' => $user->ID,
            'post_status' => array('publish')
        $lifestyle_post = get_posts ($user_lifestyle_data);
        // Get all fields with their value from current users latest lifestyle-data post
        foreach ($lifestyle_post as $post) {
            setup_postdata( $post );
                // get all meta data with key value pair
                $lifestyle_data_fields = get_fields($post);  
                //output of fields just for development purposes to see their values
                foreach ($lifestyle_data_fields as $name => $value) {
                    # code...
                    //echo '<p>Lifestyle-Data Field</p><br>';
                    echo '<p>';
                    echo '<strong>'.$name.'</strong>';
                    echo ': ';
                    echo $value;
                    echo '</p>';
     // get all analysis_textblock entries and the existing meta key and value 
        $textblocks_posts = array(
            'numberposts' => '-1',
            'author' => $user->ID,
            'post_status' => array('publish')
       /////////  HERE I AM STUCKED   ///////
       // get all textblocks_posts, 
        //Get all meta-fields
        //for each meta-field of lifestyle_data post from current user find the matching textblocks
        // each textblock found goes into an array (e.g. matching_textblocks)
       // we then loop through the array of matching textblocks and get
        // the content
        // the short-recommendation field (if present)
        // the product-relation (if present)
        // and output the data
    add_shortcode( 'related_textblocks', 'related_textblocks' );

    This is actually driving me nuts and I am not skilled enough to get it going.
    Maybe someone can help me by pointing me in the right direction on how to dynamically match all the field values. I would assume that there also performance issues to consider, where I am not sure which is the appropriate approach in our case (WP_query, meta-query, get_fields(), ….)

Viewing 1 post (of 1 total)

The topic ‘Output CPT posts based on dynamically matching ACF field value in 2nd CPT’ is closed to new replies.