WordPress[How to make relationship in two custom post types??]

I have two custom post types named products and recipes.In my product’s post type i add some products and now i want to add recipes regarding to those products.(like product P1 can have two recipes R1 and R2.I want to show those recipes button on product post type detail page.So that i can view recipes of those products.

Here is the code i created product post type and to view on frontend.

Creating Product Post Type-

add_action('init','maza_theme_menu');
add_action( 'init', 'create_post_type' )
function create_post_type() {
register_post_type( 'mazaproducts',
    array(
        'labels' => array(
            'name' => __( 'Mazaproducts' ),
            'singular_name' => __( 'Mazaproduct' )
        ),
    'public' => true,
    'has_archive' => true,
    )
);
}

Displaying All Product Post Type-

<?php
get_header();
 $loop = new WP_Query( array( 'post_type' => 'products', 'posts_per_page' => 10 ) ); ?>
<?php //echo $GLOBALS['wp_query']->request ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>
<div class="entry-content">
    <?php //the_content();
            $content = get_the_content();
            $content = strip_tags($content);
            echo substr($content, 0, 100);
     ?>
    <div class="col-md-4 col-sm-4 col-xs-12">
        <div class="product_details_thumb mar_bot">
        <?php   
            if ( has_post_thumbnail() ) {
                the_post_thumbnail();
            } 
        ?>
        </div>
    </div>
</div>

Thanks

Read more here: WordPress[How to make relationship in two custom post types??]

Leave a Reply

Your email address will not be published. Required fields are marked *