Skip Navigation

[Resolved] Display related posts on a taxonomy term archive

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

Problem: I use a custom taxonomy "Directors" applied to a CPT "Movies". I also have a custom post type "Directors". I would like to be able to show all the movies by each Director in my Directors taxonomy archive, as well as information from the Director CPT. I would also like to be able to show all the Movies by the current Director in the Director CPT single post.

Solution:
- Create Director CPT posts and Director taxonomy terms such that each Director CPT has a corresponding Director Term. The slugs should always match.
- Associate the Director term with all Movies by that Director.
- On the Director taxonomy archive, insert a View of Movies filtered by Director taxonomy term, set by a shortcode attribute. The term slug can be determined using the wpv-taxonomy-archive shortcode.
- On the Director post Layout, insert a View of Movies filtered by the Director taxonomy, set by a shortcode attribute.
- Fill in the value of the shortcode attribute with the current Director post's ID by using a custom shortcode to convert the current Director's post slug into its post ID. The slug of the current Director post will match the slug of the same Director taxonomy term, so the View of Movies will show the correct results.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 22 replies, has 2 voices.

Last updated by ritaV 6 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#609309

Hi! it's possibile display a specific custom post in a specific custom taxonomy?
How can i do it?

I use divi, toolset for create my cystom post and custom taxonomy and WPML.

i have:
1. custom post = movies
2. custom taxonomy of this custom post = directors
3. if i click on taxonomy "directors" i see all movies with this directors

but now I'd like to insert custom fields for my directors (sex, date of birth, biography ...).
i try to create custom fields for Taxonomies (https://toolset.com/documentation/user-guides/term-fields/) ad display it (https://toolset.com/documentation/user-guides/displaying-wordpress-term-fields/).
and all run ok! (i had opened a ticket here: https://toolset.com/forums/topic/add-custom-fields-in-a-custom-taxonomy-with-toolset/#post-609298)

but with this solution i cannot translate the custom fields for custom taxonomy and i cannot display the custom fields with condition.

So... i think...
if I create a new custom post (directors-post), I can add all custom fields (sex, date of birth, biography ...), show them only if they are not empty and translate them. And this is perfect. But I would also like to show all the films that the director directed (filmography). The filmography, however, is in the archive of the custom taxonomy of my movies.
How can I do?

I tried to set up some layouts and I understood that the solution would be to create a layout for every single custom post (directors-post) with the archive of custom taxonomy (directors) of movies. But this process seems confusing, not automatic and cumbersome! Is there a solution with a shortcode?

Thanks in advance

#609481

Hi, here's one way it could work:
- Keep the Directors taxonomy. Add a term for each director, and use this taxonomy on the Movies post type to associate Directors to Movies.
- Create a Director Post for each Director. When you add the post title, make sure it creates a slug that matches the director taxonomy term slug.
- Create a View of Movies filtered by the Director Taxonomy, where the term is set by a shortcode attribute like "wpvdirector".
- Insert that View in a Layout for all Directors, and use a shortcode attribute to pass in the current Director Post's slug.
- Now the View will show all Movies associated with the term that matches the current Director.

For example, let's say you have a Director Post for Steven Spielberg. The slug of that post must be "steven-spielberg". You also have a Director Taxonomy Term for Steven Spielberg. The slug of that term must be exactly the same, "steven-spielberg". Then you will insert the View in the Layout for Directors using code like this:

[wpv-view name='your-movies-view' wpvdirector='[wpv-post-id id='$current_page']']

It will work for all Directors, but only if the term slugs and post slugs are identical. If the slugs are not identical, there are other ways to link them that require a bit more work and a custom field.

The technique I'm describing is called "passing arguments into a View". It can help you modify a View based on the page where it's placed or other context. More info about that here:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

#609696
2018-01-25-12-19-localhost.png

Ok.
I'm trying your solution in a local copy of my site.
- Keep the Directors taxonomy. Add a term for each director, and use this taxonomy on the Movies post type to associate Directors to Movies. (already done)
- - Create a Director Post for each Director. When you add the post title, make sure it creates a slug that matches the director taxonomy term slug.

I'm trying this passage. But if I create a new custom post type (director / director) it comes out an error message that the slugs correspond with the custom taxonomy.

I misunderstood something?

#609847

I mean the slugs of each Director post must match the slugs of each Director taxonomy term. The slug of the post type and the slug of the taxonomy should be different. For example:
Post Type: Directors CPT - slug "director-cpt"
- Director Post 1: Steven Spielberg - slug "steven-spielberg"

Taxonomy: Directors - slug "director"
- Director Term 1: Steven Spielberg - slug "steven-spielberg"

It's okay to have a post and a term with the same slug, but it is not okay to have a post type and a taxonomy with the same slug. I hope this helps clear things up.

#609914
types of layout views.png
type of layout.png
slug film.png
slug regista coreano CPT.png
slug regista taxonomy of film.png
2018-01-25-18-39-localhost.png
tutte seguenti.png

OK! I got it. sorry for my mistake.
I get it. my cpt of director must have the exact same slug of the custom taxonomies directors of my movies.

now ... I'm trying to make every step you've described but I'm stuck and I can not move on.
Can you help me and support me in this project?

I would be extremely grateful.
I'm trying but I can not.
For the moment I'm trying to find out if what comes out is really what my client wants.

So...

1. i have created a custom taxonomy (with slug "regista" - director in english) for my first custom post (the slug of my CPT is "film") [i have insert 2 screesnhot of two slugs]
2. i have created another CPT for all Director (with slug: "regista-coreano") [1 screenshot]
3. i have created 3 custom fields for for CPT of Director (sex, date of birth and biografy)
now i have to:
----> Create a View of Movies filtered by the Director Taxonomy, where the term is set by a shortcode attribute like "wpvdirector".
and i follow this tutorial. https://toolset.com/documentation/user-guides/filtering-views-by-taxonomy/

So..
4. i add a new view and i give a name (with slug: "view-of-movies-filtered")
5. i select that this view must display for CPT film
6. i add a filter query (tassonomia - registi)
7. i select "all director" (i have 2 values with "all director.." i select the 3 values not first..) [screenshot]
8. in my test i select the first directors not all (then if all work i checked all directors)

now i have to:
----> Insert that View in a Layout for all Directors, and use a shortcode attribute to pass in the current Director Post's slug.
and i read this tutorial: https://toolset.com/documentation/user-guides/passing-arguments-to-views/

but it is not clear to me how to proceed and if all the previous steps are right..

9. i add a new layout (with slug: " layout-prova-registi-cpt")
10. i choose a Views type lauout: i select a VIEWS LAYOUT [screenshot]
11. i choose an existing views and select the views with slug: "view-of-movies-filtered" that i have created in the step 4

12. in the first box of this views i have insert:
[wpv-view name='view-of-movies-filtered' wpvregista-coreano='[wpv-post-id id='$current_page']']
a custom versione of your example:
[wpv-view name='your-movies-view' wpvdirector='[wpv-post-id id='$current_page']']
[screenshot]

am I wrong?

#609963
delete-view-shortcode.png
shortcode-attribute.png

now i have to:
----> Insert that View in a Layout for all Directors, and use a shortcode attribute to pass in the current Director Post's slug.

Okay if you want to use a shortcode attribute, you must edit the View Filtro Query (Query Filter) and choose the option shown in shortcode-attribute.png. This will allow your filter to respond to a shortcode attribute. The option you selected before should be used for a static Director filter, not a variable Director filter. You need a variable filter to work on all the different Director posts.

10. i choose a Views type lauout: i select a VIEWS LAYOUT [screenshot]
It is not currently possible to use shortcode attributes with a View Cell. Delete the View Cell and insert a Visual Editor Cell instead. Insert the View 'view-of-movies-filtered' inside the Visual Editor cell by clicking "Campi e viste" (Fields and Views). Then you can add the "wpvdirector" shortcode attribute to the wpv-view shortcode. Use the shortcode wpv-post-slug to get the current Director Post slug, and insert that as the value of the wpvdirector attribute like this:

Current Director slug: [wpv-post-slug id='$current_page']<br />
Current Director's Movies View:<br />
[wpv-view name="view-of-movies-filtered" wpvdirector="[wpv-post-slug id='$current_page']"]

12. in the first box of this views i have insert:

[wpv-view name='view-of-movies-filtered' wpvregista-coreano='[wpv-post-id id='$current_page']']

It looks like you have placed the View "view-of-movies-filtered" inside itself, which is a problem! Delete the wpv-view shortcode from the editor dell'output delciclo. See delete-view-shortcode.png. Then the code inside the Loop Output editor should begin to show Film information filtered by the Director taxonomy, term slug supplied by the current Director post.

#610386
filtro query.png
layout prova registi cpt.png
assign.png

Ok...
So...
---> Insert that View in a Layout for all Directors, and use a shortcode attribute to pass in the current Director Post's slug.
I edit the View Filtro Query (Query Filter) and choose the option shown in shortcode-attribute.png, see my new shortcode "filtro query.png"

9. i add a new layout (with slug: " layout-prova-registi-cpt")
10. i choose a Views type visual editor
11. i add in visual editor:

<p>[wpv-view name="view-of-movies-filtered"]<br />
Current Director slug: [wpv-post-slug id='$current_page']<br />
Current Director's Movies View:<br />
[wpv-view name="view-of-movies-filtered" wpvdirector="[wpv-post-slug id='$current_page']"]</p>

see the screenshot "layout prova registi cpt"

12. i assign the "layout prova registi cpt" to my registi CPT "with slug "regista-coreano") [shortcode "assign"]
and before i add custom field to this layout.

But... don't run...

#610390
regista cpt.png
regista taxonomy.png

If i go to
regista cpt (../regista-coreano/ahn-byeong-ki/): see screenshot "regista cpt"
regista taxonomy (../regista/ahn-byeong-ki/): see screenshot "regista taxonomy"

I would like that under the "director cpt" there was "taxonomy director" with the archive of filmography.
and if i click on the director taxonomy (present in my movie CPT) would see the CPT director page with custom field and under the filmograpy.

Did I explain myself correctly?
Did you understand earlier?

#610458
2018-01-28-14-10-webrelativo.it.png

Oh wow! Now it run! I see all filmography in my cpt directors! 🙂
(I just had to insert the loop! thank you so much!)

Now
in theory I guess I should do the same thing to show directors (cpt) in directors (custom taxonomy of movies).

I know ...
I tried to create a new view to show the directors (cpt), but when I go to apply the query filter I do not know what to insert ... Do I have to apply the query filter of the taxonomy directors?

otherwise I know that the alternative is a massive redirect of url from the taxonomy to the directors' custom post. but it would be a solution that I wanted to avoid ...

#610477

It is a bit more difficult to go in the opposite direction because there is no way to filter Directors CPT posts by post slug. So you must determine the Director CPT post ID using the taxonomy term slug and a bit of custom code, and then filter your View using this post ID.
- Create a new View of Director CPT posts, filtered by post ID, set by a shortcode attribute "ids"
- In the Loop Output, insert the post title to show the Director's name
- While viewing a term archive, you can access the current taxonomy term slug using the wpv-taxonomy-archive shortcode:

[wpv-taxonomy-archive info='slug']

- We must convert that slug into a post ID for the View's filter to work correctly. Add the following custom code to your child theme's functions.php file:

function get_id_by_slug_and_type_func($atts) {
  $page_slug = $atts['slug'];
  $post_type = $atts['type'];
  $page = get_page_by_path($page_slug, OBJECT, $post_type);
  if ($page) {
    return $page->ID;
  } else {
    return null;
  }
}
add_shortcode('get_id_by_slug_and_type', 'get_id_by_slug_and_type_func');

- The shortcode is used like this:

[get_id_by_slug_and_type slug='director-post-slug' type='director-type-slug']

- Combine the wpv-taxonomy-archive and get_id_by_slug_and_type shortcodes to pass the director post ID into the View's "ids" shortcode attribute, and insert the View into a WordPress Archive of your Directors Taxonomy:

[wpv-view name="your-director-post-view-slug" ids="[get_id_by_slug_and_type slug='[wpv-taxonomy-archive info='slug']' type='director-type-slug']"]

- Replace "your-director-post-view-slug" with the slug of the Director CPT View
- Replace "director-type-slug" with the Directors post type slug

#611590
ops.png
post id filter.png
loop post title.png

Oh.. Uff something went wrong and not as I expected.

So....
my detailed steps:

1. I Create a new views, a Director CPT View (with slug: "vista-cpt-registi-in-tag")
This View will display: Director Post Type
I add filter for post ID
and I set the filter type for id as "Posts with IDs set by the View shortcode attribute" (see screenshot "post id filter")
In the loop output, enter the post title to show the director's name (see screenshot "loop post title")

2. I create a new layout (not a new view) (with slug: "layout-archivio-registi")
i Assign to content to display taxonomy director archive ("This layout is used for these loops: Regista archives" and "regista" is my custom taxonomy of Movies) I insert a visual editor cell with this shortcode:

[wpv-view name="your-director-post-view-slug" ids="[get_id_by_slug_and_type slug='[wpv-taxonomy-archive info='slug']' type='regista-coreano']"]

- I Replace "your-director-post-view-slug" with the slug of the Director CPT View (vista-cpt-registi-in-tag)
- I Replace "director-type-slug" with the Directors post type slug (regista-coreano)

so i insert this exactly shortcode:

<p>[wpv-view name="vista-cpt-registi-in-tag" ids="[get_id_by_slug_and_type slug='[wpv-taxonomy-archive info='slug']' type='regista-coreano']"]</p>

3. i add your code to my file functions.php.

if i go on link of my director custom taxonomy I see only a symbol:

“]

(see screenshot "ops")

thanks in advance for the patience you are having. I'm learning a lot and toolset really has a lot of potential.

#611860

Okay let's try to separate all the shortcodes and see what's working and what's failing. Please place this code in your Layout:

Archive slug: [wpv-taxonomy-archive info='slug']<br />
ID by slug and type: [get_id_by_slug_and_type slug='[wpv-taxonomy-archive info='slug']' type='regista-coreano']<br />
View: [wpv-view name="vista-cpt-registi-in-tag" ids="[get_id_by_slug_and_type slug='[wpv-taxonomy-archive info='slug']' type='regista-coreano']"]

Let me know the results on the front-end.

#611898
backend.png
frontend.png

here I am! I send you the two screenshots: backend and frontend!

#611901
backend vista-cpt-registi-in-tag.png

the issues is on the views...
so... a screenshot of backend of my view: "Vista cpt registi in tag"

#611931

Okay I overlooked one step. Go to Toolset > Settings > Frontend Content and scroll down to "Third-party shortcode arguments". Type "get_id_by_slug_and_type" to add the custom shortcode here. This should fix the issue displaying the View results. Try again and let me know.