[Resolved] Display the title links to parents of different CPTs

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 6 replies, has 2 voices.

Last updated by Gavin 5 years, 9 months ago.

Assigned support staff: Adriano.

Author
Posts
#226844

Hello

I'm stuck.

I have set up a many-to-many relationship with four CPTs, for example:

CDs, DVDs and Singers are all parents of Contributor (where Contributor is the intermediary post type).

So... a Singer can contribute to multiple CDs and multiple DVDs and a CD or DVD can have many singers.

I have set up a view to list all the Singers on a CD and inserted it into a content template for a CD. This works fine. I do this by setting the content selection to Contributor, filtering by Post Relationship: Post is child of 'Post where the view is inserted' and in the wpv-loop display the title with the id="$singer". All good. e.g.

Singer 1
Singer 2
Singer 3

So, now I want to list all the CDs and DVDs for a Singer with links to the respective CDs or DVDs. I do not want to display the Contributor title but the title of the CD or DVD CPT. This is where I have a problem.

I have set up a view to list all CDs and DVDs. I have set the content selection to Contributor filtering by Post Relationship: Post is child of 'Post where the view is inserted'. I get the correct message under the filter: "Post type Contributor is a child of CD, DVD and Singer post types".

In the wpv-loop I tried using the id="$parent" as follows:

<wpv-loop>
[wpv-post-link id="$parent"]
</wpv-loop>

I get the correct number of results but each title links to the Contributor and not to the CD or DVD.

e.g.

Contributor 1
Contributor 2
Contributor 3
Contributor 4

However, if I set the id to CD:

<wpv-loop>
[wpv-post-link id="$cd"]
</wpv-loop>

...then I get the correct CD titles and links but the DVDs link to the Contributor.

e.g.

Contributor 1
Contributor 2
CD 1
CD 2

And if I set the id to DVD:

<wpv-loop>
[wpv-post-link id="$dvd"]
</wpv-loop>

...then I get the correct DVD titles and links but the CDs link to the Contributor.

e.g.

DVD1
DVD2
Contributor 3
Contributor 4

Please can you tell me where I am going wrong? Many Thanks.

#226900

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Gavin,

I have another idea for you, let's see if it works for you.

1) Make Contributor parent of all post types. So will be stored in each child a custom field called "_wpcf_belongs_contributor_id" which will be the ID of the intermediary post type
2) Now let's create a view that will be displayed in the Singer Content Template
3) This view will display CD and DVD post type
4) Now let's insert a filter in functions.php to show only posts with the same "_wpcf_belongs_post_id" meta field value. Here you go:

add_filter('wpv_filter_query', 'same_parent_func', 10, 2);
function same_parent_func($query, $settings) {
    if($settings['view_id'] = 123)
    {        
        $query['meta_query'][] = array(
        'key' => '_wpcf_belongs_contributor_id',
        'value' => get_post_meta(get_the_ID(), '_wpcf_belongs_contributor_id', true),
        'compare' => '='
        );
    }
    return $query;
}

You must replace 123 with the ID of the view, also replace "contributor" with the right slug name of the post parent.

Please let me know if you are satisfied with my answer and if I can help you with any other related question.

#226991

Hello. There was a more elegant solution:

<wpv-loop>
<li>
[wpv-if par="_wpcf_belongs_cd_id" evaluate="!empty($par)"][wpv-post-title id="$cd"][/wpv-if]
[wpv-if par="_wpcf_belongs_dvd_id" evaluate="!empty($par)"][wpv-post-title id="$dvd"][/wpv-if]
</li>	
</wpv-loop>
#227012

Actually, the code I pasted does not work as the fields are always populated. The correct code is:

<wpv-loop>
<li>
[wpv-if par1="_wpcf_belongs_cd_id" evaluate="$par1 != 0"][wpv-post-link id="$cd"][/wpv-if]
[wpv-if par2="_wpcf_belongs_dvd_id" evaluate="$par2 != 0"][wpv-post-link id="$dvd"][/wpv-if]
</li>	
</wpv-loop>

BUT it's worth remembering NOT to use - in slugs as these do not get passed correctly. e.g. Do NOT use my-dvds or double-cds but use mydvds or double-dvds

#227013

(sorry, in that last sentence the last word should be doublecds )

#227041

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Nice, thanks for sharing.

#227049

Thanks too! 🙂 Really enjoying using Types Toolset. I think it's worth pointing out in the documentation to use slug names without hyphens. I did report this and it might get fixed but it caught me out again - lol. I have now changed all slugs to single words.