Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other WordPress – Creating hierarchy between separate custom post types

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #38332
    Ixolite
    Participant

    Hi,

    I’m trying to create a hierarchy like structure using non-hierarchical custom post types.

    What I am trying to achieve is to have a ‘chapter’ and ‘book’ custom post types where several ‘chapter’ pages can be linked to selected ‘book’ page.

    I can display a list of chapters inside a ‘book’ post easily with this code: http://pastebin.com/jdeqi0rR

    First I check if meta checkbox is selected, indicating that the ‘book’ has chapters linked to it.

    If it is, I run a loop to output all ‘chapter’ custom posts that have meta box value equal to current ‘book’ post slug value. That is the way I am linking the ‘chapter’ to ‘book’ – by inputting book’s slug into chapter’s meta box.

    So listing chapters within a book is simple and works fine. However, I would also like to display book’s meta values from within ‘chapter’ post and that’s where I’m stumped.

    I cannot figure out a way to get to ‘book’ custom post data by using values that are stored on the ‘chapter’ side. I want to be able to point the ‘book’ post by providing some meta value in ‘chapter’ post.

    Any ideas?

    #103856
    Ixolite
    Participant

    OK, I figured it out – bad case of brain fart I guess. It was as simple as creating a query to display ‘book’ custom post based on the meta key value from the ‘chapter’:


    $parent_name = get_post_meta($post->ID, 'ecpt_chapter_parent', true); //getting the 'book' post name value from current 'chapter' post
    $temp = $chapter_query;
    $chapter_query= null;
    $chapter_query = new WP_Query();
    $chapter_query->query('name='.$parent_name.'&post_type=any');
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Other’ is closed to new topics and replies.