Skip Navigation

[Resolved] Need to group post type by a parent of the same type

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

Problem:
Need to group post type by a parent of the same type

Solution:
Self-join relationship is not supported yet with Types.

There workaround - You should add a dropdown select field to your custom field group and fill its value dynamically.

Once you created the field - then you should use the following hook to populate the dropdown select options dynamically.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/need-to-group-post-type-by-a-parent-of-the-same-type/#post-1175089

Relevant Documentation:

This support ticket is created 5 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 3 voices.

Last updated by alexG-4 5 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1174913

I have a set of products. Some of them are a parent to other products. I need my /product page to show all of the products, but arrange them in groups. I guess you could think of some of the products as product-groups but I don't want the groups (parent posts) to have a different url structure or setup. They are the same in every way, except some are parents to other products.

- I tried creating a relationship but you aren't allowed to relate one type to itself.
- I tried to make a related post custom field but it doesn't allow linking to the same type.

I can think of ways to do this that are more fragile but I want a solution that the client is less likely to break.

What is the link to your site?
hidden link

#1175089

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - self-join relationship is not supported yet with Types.

- I tried creating a relationship but you aren't allowed to relate one type to itself.
- I tried to make a related post custom field but it doesn't allow linking to the same type.
=> its not possible to create a self join relationship.

I think I have a workaround to suggest - You should add a dropdown select field to your custom field group and fill its value dynamically.

Once you created the field - then you should use following hook to populate the dropdown select options dynamically.

For example - Create a custom select field namely "select products" with your custom field group and then added following code to your current theme's functions.php file that is used to populate the dropdown with existing essay post type entries :

add_filter( 'wpt_field_options', 'func_dynamic_populate_essey', 10, 3);
function func_dynamic_populate_essey( $options, $title, $type ){
    switch( $title ){
        case 'select products':
            $options = array();
            $args = array(
                'post_type' => 'product',
                'numberposts' => -1,
                'post_status' => 'publish');
 
           
            $posts_array = get_posts( $args );
 
            // adding default option  
             $options[] = array(
                    '#value' => 0,
                    '#title' => "Please Select",
                );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
            break;
    }
    return $options;
}

If you add/edit the essay post - you will see the "select product" select field is available to select the options and you can save the ID of the selected option with custom field "select product".

#1225359

Hi Minesh

Just want to say "thanks" for this great answer.

I have the exact same problem and you've provided me with exactly what I need to solve it.

Good job!

Alex