Skip Navigation

[Resolved] Filter view of selections from auto-populated checkboxes field

This thread is resolved. Here is a description of the problem and solution.

Problem:
The problem in this post essentially comes down to filtering a view by a custom field list value.

Solution:

Views can be filtered by several Attributes which includes a shortcode attribute, where you can pass the value of your custom field to the view.
https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 18 replies, has 3 voices.

Last updated by curtZ 6 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#597473
Screen Shot 2.png
Screen Shot 1.png

I am trying to:
We are auto-populate a checkboxes field with a custom post type title using a view.

However, in the front-end all of the checkbox options are shown instead of just the selections.

We want only the selected checkbox options to show.

Thank you in advance for any help with this.

#597504
Screen Shot 2017-12-10 at 3.42.20 PM.png

Okay it looks like your checkbox options are automatically populated by your custom post type Providers. I assume the value of each option is the same as the Provider ID, correct? So the value of the Dan Akroyd option is the ID of the Dan Akroyd post, and the selected values are being stored as a repeating custom field on the Practice post type, by using the cred_save_data hook and custom PHP. If I'm right about those assumptions, here's how this should work.

On the front-end (screenshot 2) I assume this is the Practice post or loop of Practice posts, and you are showing a View of Providers. You want to filter the View of Providers to show only the selected Providers for this Practice. So you must add a Query Filter to the Providers View. Set it to filter by Post ID, where the Post ID is provided by a shortcode attribute "ids". See the attached screenshot for an example.

This allows you to pass in the Provider IDs associated with the current Practice using a shortcode attribute. You know those IDs because they are stored in a custom field on the Practice post, which you can access using the wpv-post-field shortcode. Place this shortcode in your Practice single post Content Template or Template Layout:

[wpv-views name="your-providers-view-slug" ids="[wpv-post-field name='wpcf-providers']"]

Replace "your-providers-view-slug" with the slug of your Providers View, and replace 'wpcf-providers' with the slug of your providers custom field.

More information about accessing post fields and passing arguments into Views:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-field
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

Let me know if you have additional questions about this.

#597522
Screen Shot 2017-12-10 at 8.01.32 PM.png

Chrisitian,

Sorry...I'm following the logic but I don't know how to get to the window that that looks like your screenshot.

When I go to the providers view, in the filters section and click "New filter" I see the window in my screenshot -- which doesn't look like I'm in the right place.

#597525
Screen Shot 2017-12-10 at 8.05.27 PM.png

Christian,

Please disregard my last post.

I found the query options - it was unselected in the screen options pane.

#597732
Screen Shot 4.png
Screen Shot 3.png
Screen Shot 2.png
Screen Shot 1.png

Christian,

Not quite working yet.

Just to confirm from you that I understand this correctly:
- Screenshot 1 shows a part of the CRED post form with the generic field used to auto-populate a field of checkboxes with the "provider" CPT title.
- Screenshot 2 is the said checkboxes field -- this appears on the "practice" CPT.
- Screenshot 3 is the code on the "practice" Content Template (we're using the divi builder) to populate the "practice" post.
- Screenshot 4 shows a "practice" post -- the providers have not been filtered.

So I'm still doing something wrong here.

Thanks as always for your help.

#597783

You need to update the View shortcode. Remove the first line shown in screenshot 3 (Divi can't interpret the square bracket shortcodes) and add the ids parameter to the View shortcode as shown here:

{ ! {wpv-view name="providers-show-on-practice-cpt" ids="{ ! {wpv-post-field name='wpcf-providerswithpractice'} ! }"} ! }

*Remove the spaces around each exclamation point - the forum is having trouble displaying this code correctly.

Are the selected Providers stored correctly in the Practice post? You should be able to see this quite easily by editing the Practice in wp-admin. If not, we need to examine your cred_save_data hook. Please copy + paste that code here for me to review.

#597807
Screen Shot C.png
Screen Shot B.png
Screen Shot A.png

Screenshot A : Made the changes to the shortcode in the Practice content template.

Screenshot B : Now on the Practice post, the providers are listed in 2 groups, and are not filtered.

Screenshot C : The Practice edit page doesn't seem to show the selected Providers correctly.

Here is the cred_save_data hook :

/* When creating a NEW PROVIDER CPT, create the post title from the provider's first, MI, last name and degree  */
add_action('cred_save_data', 'custom_provider_title',10,2);
function custom_provider_title($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==113 && isset($_POST)) {
        // Update post {ID}
        $title = $_POST['wpcf-provider-first-name'];
        $title .= ' '.$_POST['wpcf-provider-middle-initial'];
        $title .= '. '.$_POST['wpcf-provider-last-name'];
        $title .= ', '.$_POST['wpcf-degree'];
           
          $my_post = array(
              'ID'           => $post_id,
              'post_title'   => $title,
              'post_name'   => $title
                 
          );
   
        // Update the post into the database
          wp_update_post( $my_post );
           
    }
}

...and the raw output for the populated checkboxes ( you didn't ask for this, but in case it helps)

/* Have raw text output just like json.*/
add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
    
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '124' ) { //Adjust to your Views ID
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

Thank you!

#597859

Okay I see the disconnect. I've been under the assumption that the Provider checkboxes are used to set the value of a custom field on the Practice post, like a repeating numeric field that holds the selected Provider IDs. However, in Screenshot C you are showing the Post Relationship area, which is used to manage child posts of the Practice. These are very different concepts so let's be sure we're on the same page.

Do you want the Providers selected in your screenshot "B" to be stored as IDs in a repeating custom field on the Practice post, or should each selected Provider be created as a child post of the Practice? It appears that you have up the Practices and Providers for the latter option - post created as a child of the Practice. However, I was providing guidance for the former option.

#597867

Ah, I see. Sorry Christian. The Provider CPT is a child of the Practice CPT.

#597868

I'd like to be able to create individual Provider posts, then, when I create the Practice post, to be able to select (with the checkboxes) the Provider(s) that belong to that Practice.

#597872

Okay so a Provider can only belong to a single Practice, is this correct?

#597873

A Provider should be able to be listed in more than one Practice.

#597879
shortcodes.png

I see. In this case a parent / child relationship between Practice and Provider is not appropriate, because a child cannot belong to more than one parent from a single post type. Instead, you should create a custom field on the Practice post type. This field should be a numeric field that stores a Provider ID. Configure the field to accept multiple values. This way you can store multiple Provider IDs to associate with the Practice. Then later when you want to display all the Providers, you can loop over these custom field values and get information about the related Provider posts.

add_action('cred_save_data', 'custom_provider_title',10,2);

Okay this looks like the save data hook used when you create a Provider post. We need to add another save data hook that will be triggered when you create a Practice post, because this is where we need to add some custom functionality. Access the values selected in your generic field in the $_POST object, and loop over those selected values. Use update_post_meta to add each values to the custom field in your Practice post. Here's an example:

add_action('cred_save_data', 'cred_save_data_action',10,2);
function cred_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12345)
    {
        if (isset($_POST['your-generic-checkboxes']))
        {
           foreach($_POST['your-generic-checkboxes'] as $provider_id) {
              add_post_meta($post_id, 'wpcf-yourfieldslug', $provider_id);
            }
        }
    }
}

Change 12345 to match your form ID, change 'your-generic-checkboxes' to match the name of your generic checkboxes field, and change 'wpcf-yourfieldslug' to match the slug of the repeating custom field you added to the Practice post type. Use the 'wpcf-' prefix, followed by the slug from the post field group editor. So if your field slug is "provider-id" then you should use "wpcf-provider-id".

Refer to the screenshot attached here. You must delete some code in your page builder to remove the duplicate Views on screenshot B above.

#598056
Screen Shot A7.png
Screen Shot A6.png
Screen Shot A5.png
Screen Shot A4.png
Screen Shot A3.png
Screen Shot A2.png
Screen Shot A1.png

Thank you Christian.

1. Practice/Provider, parent/child relationship : I removed the relationship.

2. I created a numeric custom field on the Practice post type, with the field slug "provider-id" (screenshot A1)

3. I added this field to the Practice post type (screenshot A2)

3. Based on the example you provided, I placed the following cred_save-data in functions.php (screenshot A3) :
-- The form ID is the form I use to add a practice (screenshot A4); the form in which the Provider checkboxes reside.
-- The 'your-generic-checkboxes' is the name of the generic checkboxes field (screeshot A5), namely 'providerswithpractice'
-- In the code, I changed add_post_meta to update_post_meta, as I assumed from your guidance

/* When creating a NEW PRACTICE CPT, Access the values selected in the generic field in the $_POST object, 
and loop over those selected values. Use update_post_meta to add each value to the custom field in the Practice post.  */

add_action('cred_save_data', 'cred_save_data_action',10,2);
function cred_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==178)
    {
        if (isset($_POST['providerswithpractice']))
        {
           foreach($_POST['providerswithpractice'] as $provider_id) {
              update_post_meta($post_id, 'wpcf-provider-id', $provider_id);
            }
        }
    }
}

After doing this, when I created a Practice with just Dan and Jerry selected, it still displays all providers in the front end (screenshot A6).

In case it helps, in the Practice edit page, the Provider ID field is empty (screenshot 7).

Thank you.

#598202

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Curt,

As Christian is currently on Vacation I will be handling this ticket.

Taking a look at this it seems you are having trouble with displaying the correct amount of Practice for a provider.

Would you mind providing me with admin access to the website so I can run through the setup to see if there is something that we might have missed in assisting you with this setup ? Maybe your view isn't filtering the information correctly.

The private fields have been enabled.

Thanks,
Shane