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
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.
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
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
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.
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
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)
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
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.
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
Nice, thanks for sharing the solution.