WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show different custom post types related by Taxonomy (2 posts)

  1. Twentyfourten
    Member
    Posted 1 year ago #

    Bit of a challenge this one.

    A Golf Holidays website has Custom Post types of 'resort' and 'course'.
    A Taxonomy called 'destinations' allows the CPTs to be assigned to hierarchical taxonomy terms which represent Countries and Regions of a Country e.g. 'Portugal' (parent term) and 'Algarve'(child term).

    If in a 'Resort' post, I wanted to say "...and here are the Courses nearby..." and display a list of the 'course' custom post types within the same taxonomy term as the current post, what structure would my loop have to take?

    clarification of 'Nearby':
    The Loop checking to see if first the Resort Post belongs to a child term, and displaying the Course Posts which match the same term, and then if current Resort Post belongs to no child term, then displaying Course Posts for the same parent term.

    Would appreciate any ideas of how to approach?

    Cheers

  2. Twentyfourten
    Member
    Posted 1 year ago #

    Answering my own question.

    Searched endlessly for 'related' plugin's that might allow this. All 'related' plugins seem to presume you are searching against related terms and taxonomies within your posts 'type' and not for another 'custom post type'.

    Forum suggestions even made me reconsider my SETUP logic altogether, and have just one post type and 'tag' as 'Resort' or 'Course'. But I definitely needed the two separate CPTs, as they were different by content (different custom fields) and not just name.

    So, I achieved the desired GOAL by combining a couple of plugins.

    1 - Display Posts Shortcode
    2 - advanced custom fields/
    3 - advanced custom fields - taxonomy add-on

    1 - Allowed me to put shortcode in my Post admin, and display manually on every post something like this:

    [display-posts taxonomy="destinations" tax_term="algarve" post_type="course"]

    2 and 3 - Allowed me to create a custom field which would be pre-populated with each posts terms in a hierarchical checkbox and shown in the posts admin. This allows complete flexibility over defining the 'Location' that is meant by 'Nearby'.

    That is output then as this:

    <?php the_field('define_nearby'); ?>

    Outside of post admin and within the template/page for each CPT I simply combine the two plugins as shown below, and this then shows all the 'Related posts' for a different Custom Post Type in the current posts location.

    [display-posts taxonomy="destinations" tax_id="<?php the_field('define_nearby'); ?>" post_type="course"]

    I'm sure there are many different ways of producing this, a Custom Loop perhaps? Anyway, for me this achives exactly what I wanted.

    Hope it helps someone else to spend much less time than I did looking for a solution.

Topic Closed

This topic has been closed to new replies.

About this Topic