Grow your CSS skills. Land your dream job.

WordPress – Creating hierarchy between separate custom post types

  • # June 3, 2012 at 5:58 pm

    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?

    # June 4, 2012 at 4:44 am

    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)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".