Skip Navigation

[Résolu] Post reference to existing content

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created Il y a 9 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 -
- - - - - - -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 10 réponses, has 2 voix.

Last updated by Adriano Il y a 9 années et 9 mois.

Assisted by: Adriano.

Auteur
Publications
#219180

Hi,
I'm building a conference website, I have 2 custom types: Courses and Trainers.

I created courses and added trainers. Now I would like to reference the trainers to the course. So I would like to add an existing trainer or more trainers to the course and setup template for that, so on course page I would see the course title, description + trainers pictures and names linked to trainers page.

On trainers page I would like to have trainer picture and description and automatically referenced list of courses of this trainer.

How can I build this kind of website with wp types?

I was reading the relationships and many to many relationships tutorial, but it wasn't helpful. With the relationship setup I can only add a new content to another content type, there is no way to reference an existing content. With "many to many relationships" I could create 3 types: course, course topic and trainer and then for course set the topic and the trainer as parents of course. With this setup I could reference for the course the right topic and the trainer, but only one trainer and some courses have 2 or more trainers.

I created the similar site very easy with Drupal using Views and Node Reference modules I'm wondering how can I make it with WordPress.
Here the links to my Drupal website:

Course Page: hidden link

Trainer Page: hidden link

I would really appreciate any help.
Regards,
Michael

#219270

Dear Michael,

I will write below some procedures you should follow to achieve what you want:

1) Create three custom posts (I think you already created): Courses, Trainers and Course Topic.
2) Make Courses and Trainers child of Course Topic, both
3) Let's create the view to list the trainers related to the current course. Create a normal view to list Trainers, you don't need to choose a filter and then add the code below in functions.php:

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

You must replace "coursetopic" with the slug name of Course Topic post type and also 123 with the ID of the created view. You will replace 456 in the future with the ID of the second view.

4) Insert this view in the Content Template of Courses
5) Let's create the view to list the courses related to the current trainer. Create a normal view to list Courses, you don't need to choose a filter and then replace 456 in the code above with the ID of this view.
6) Insert this view in the Content Template of Trainers

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

#225436

Hi Adriano,
thank you for your answer.
Is there a way to assign few trainers to my existing course, something like custom reference field. I have 30 courses and about 50 trainers. The perfect solution would be to could edit the course and see there an option to choose/mark/select from the full list of existing trainers only the trainers that belong to the course.

Concerning related content and filter in functions.php, Is there a way to do not touch theme files, is not a good practice to mix template and functionality. Is there an another place, where I could add the filter code?
Regards,
Michael

#225442

Update: ok, the many-to-many relationship solution works very well for me, I created 'trainer', 'course' and 'appearance' types, and set both ‘trainers’ and ‘courses’ as parents of appearance. I will test the filter code and let you know how it worked for me. Would be great if you could advise me, if there is an other way to add the filter code without touching the theme file(functions.php)?

Many thanks for now 🙂
Regards,
Michael

#225455

Dear Michael,

That's the only way you can do that, with coding. For now this feature was not added in Toolset GUI filters.

I will let this ticket opened waiting for your confirmation.

#225657

Hi Adriano,

it looks like your code

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

breaks all other views. It looks it tries to filter all views. I think the 'if statement' is wrong and it should be:

 if(($settings['view_id'] == 123) OR ($settings['view_id'] == 456)) 

The above correction prevent breaking another views but the selected views still show "no items found".

Are you sure the rest of code is correct? I'm sure I set all the steps, you described, correctly.

Regards,
Michael

#225722

Dear Michael,

You are right, my apologies for my mistake. You code is right, but you don't need to use my 123 view, the following "if" should be enough:

if($settings['view_id'] == 456)
#225724

Hi Adriano,
yes I know I should use my own views id, but the filter still doesn't work for me, I also replaced 'coursetopic' with my intermediary object slug name, and added the view to the template and I'm still getting "no result founds".

Is the rest of code correct?

    $query['meta_query'][] = array(
        'key' => '_wpcf_belongs_post_id',
        'value' => get_post_meta(get_the_ID(), '_wpcf_belongs_reference_id', true),
        'compare' => '='
        );

Regards,
Michael

#226200

Dear Michael,

I think it would be more simple to understand where you are missing if I access you site, could you provide credentials? The private area is enabled, please fill required fields.

#227502

Hi Adriano,
i succeed to display related content without any code. I just created the view for my intermediary object, and under query files I've set "Select posts that are a children of the Post set by parent View" and added field with id of my child post. To display names of trainers I added:


[wpv-post-link id="$trainer"]

and added this view to my courses template.

Regards,
Michael

#227503

Nice, thanks for sharing the solution.

Le forum ‘Types Community Support’ est fermé à de nouveaux sujets et réponses.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.