Skip Navigation

[Resolved] Create a link on parent post that shows all children of the parent

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 9 years, 6 months ago. 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 replies, has 2 voices.

Last updated by nelsonS 9 years, 5 months ago.

Assisted by: Adriano.

Author
Posts
#253904

I have a parent post (Artist) and child posts (Albums). I want to create a front-end link in the artist post that when clicked, it takes a user to the album archive page and displays albums related to the artist.

In other words, the functionality I want to archive is exactly like the one provided by the WordPress' get_author_posts_url( get_the_author_meta( 'ID' ) ) which returns posts by the selected/clicked-on author

#253981
view filter.png

Please follow the steps below to achieve that output:

1. Create a View and call it as "Albums by Artist"
2. In the Content Selection section choose Albums
3. Add a new filter by "Post Relationship - Post is a child of" as you can see in the attached image
4. In the Layout HTML/CSS/JS section you can insert the data you want to the loop of items
5. Ok, now you have a list of Albums according the artist ID you provide via URL argument "artist"
6. Insert this View in any page
7. In the Artist page create a link like this:

<a href="page-test/?artist=[wpv-post-id]">See all albums</a>

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

#254090

Sorry for the late reply. thanks for replying Adriano but I forgot to mention that I don't have views (using the free version). Can you show me how to go about it using the PHP api solution ?

#254259

Of course. You can use types_child_posts() to achieve that. It is fully explained in this User Guide page:

https://toolset.com/documentation/user-guides/display-fields-table-content-using-views-or-the-types-api/

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

#254280

Thanks for getting back Adriano. I have to admit I have become a bit confused. Can you please rephrase your answer. This points should guide you :

1) Which file do i place the types_child_posts() code? Do i put it in the parent (artist), child(albums) or archives.php file (displays all album files by all artists) ?
2) using types api, what do i write in the link's href attribute that will take me to the list of all albums by the specific artist i.e. <a href ="<? what do i write here?>">Albums by Artist</a>

My current files
a)single-artists.php // shows single artist information
b)single-albums.php // shows single album information . child to artist
c)archive-albums.php // shows all albums
d) archive-artists.php // shows all artists

#254502
Albums.jpg
Artist.jpg

I have added some wireframes to guide anyone who wants to assist me in solving this problem. I am currently able to load the children in the parent page (Artist) but where I am getting confused is how to set up a link to archives-albums which will filter albums to only show albums by Nelson, when I click on 'See all of Nelson's Albums' .

Using the PHP solution, what do i rewrite in the href attribute to take me to the archive page/view that shows me all of Nelson's albums i.e. <a href= "what do i write here ?"> See all of Nelson's albums</a>

#254560

Ok, let me explain you how types_child_posts() works.

This function gets all child posts of the current post. So, you have to use this inside of a parent post page. If you want to show all Albums (child) of an Artist (parent), you have to insert this function in the single-artists.php file (the parent post page).

In case this parent has more than one post type as child, you must define which is it: types_child_posts('album').

Look, the code below should be in single-artists.php:

$child_posts = types_child_posts('album');
foreach ($child_posts as $child_post) {
      echo $child_post->post_title.'<br />';
}

It will just print the post_title of each album. One Album per line.

Of course, you can edit this code adding a counter to only show the first 4 Albums.

To achieve the way you want for archive is a little bit difficult, you will need some custom PHP code. I'm afraid that is not a feature of Toolset yet, and such a script would need custom coding which is beyond the scope of our support. This being the case please consider contacting one of our certified partners from this link: https://toolset.com/consultant/

But, you can show all Albums and then hide some that you want, using jQuery.

#254568

Okay Adriano , thank you . I was able to list the children under the parent, as you have said (even doing things such as limits, post format to return etc) but my attention was that the posts that appear under the parent should just be a preview and then when a user clicks a link, he or she is taken to the full list.

I have tried to play around with WordPress' add_query_arg() and get_post_type_archive_link() to pass variables to the archive-albums.php but alas, I have failed. I will see among your consultants on the person that will help me.

Thank you very much. I am not sure I should mark this as solved but I will do it anyway

#254569

Ok, you are welcome.

#254904

After some digging, I was able to get a partial solution. In summary

1) register a query_variable that will be used to hold the data that you will pass to the archives-albums.php this code goes in functions.php file

function sds_add_artist_id_var($vars){
       $vars[] = 'artist_id';
      return $vars;
}
add_filter('query_vars', 'sds_add_artist_id_var');

2). In single-artists.php, pass the post's ID to the query variable that was registered in step 1. This should be done within another wordpress function called add_query_arg() which passes together with a declared URL. Another wordpress function called get_post_type_archive_link will be used to get the albums' archive page.

$artist_identifier  = array('artist_id'=> $post->ID); //pass post id to artist_id query variable
$album_archives_link = get_post_type_archive_link('albums') // will go to archive-albums.php

//then on the link that user will use to access all albums that belong to the artist
<a href="<?php echo add_query_arg($album_identifier, $album_archives_link)?>"> All Albums by Nelson</a>

The ablove code will basically produce something like hidden link . The next step utilizes this to filter the album information to display only albums that belong to the artist whose id is 51 . this is done in archive-albums.php

<?php
//get the query variabe, the wordpress way

$one_artist = get_query_var('artist_id') // when echoed it will produce  a string with the value '51'. you can sanitize it appropriately 

/*check if the query variable isn't empty. if it isn't empty then run query that will return only albums that belong to the artist otherwise, display all albums. you can also write the logic in a way that if a person manually enters an ID that does not exist, then it should default to showing albums from all artists */


if (!empty($one_artist)){
//variables that will be passed to a query
$args = array(
       'post_type' => 'albums',
       'meta_query' => array(
             array(
                 'key' => '_wpcf_belongs_artists_id',
                 'value' => $one_artist 
            )
       )
);

$single_artist_query = new WP_QUERY($args);

      if ($single_artist_query->have_posts() ){
            while($single_artist_query ->have_posts() ):
                $single_artist_query ->the_post();
?>
      <div class="album_item">
          <p><?php the_title();?></p>
     </div>
<?php          

            endwhile;
 
     }else{ //this is for when person enters an artist's ID that doesn't exist
       //TODO
   }


}else{//this when the variable is empty or not passed
 // TODO
}
  
#254905

Continuing from above, the reason I have said it is partiable is because I haven't figured

1) a way to extract parent information of the albums outside the loop
2) URL rewrite to make the URL prettier

I might create a new thread so that someone can help me solve this

The forum ‘Types Community Support’ is closed to new topics and replies.

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