How to generate programmatically a post which displays a chunk of content from two different posts belonging to two different Custom Post Types?

To display content from two Custom Post Types (CPTs), like “Books” and “Authors,” on a third CPT (e.g., “Quotes”), follow these steps:

1. Create a “Quotes” Post Type

In functions.php, register a new CPT:

function create_quote_post_type() {
    register_post_type( 'quote',
        array(
            'labels' => array(
                'name' => __( 'Quotes' ),
                'singular_name' => __( 'Quote' )
            ),
            'public' => true,
            'has_archive' => true,
            'supports' => array( 'title', 'editor' ),
            'rewrite' => array( 'slug' => 'quotes' ),
        )
    );
}
add_action( 'init', 'create_quote_post_type' );

2. Add ACF Relationship Fields

Using Advanced Custom Fields (ACF), add two relationship fields to the “Quotes” CPT:

  • One linking to “Authors”
  • Another linking to “Books”

3. Display Combined Content

In the single-quote.php template, retrieve data from the linked posts and display it:

<?php
$author = get_field( 'author_relationship_field' );
$book = get_field( 'book_relationship_field' );

if ( $author && $book ) {
    $author_name = get_the_title( $author->ID );
    $birth_year = get_field( 'birth_year', $author->ID );
    $book_title = get_the_title( $book->ID );
    $book_quote = get_field( 'quote', $book->ID );

    echo "<p>{$author_name}, born in {$birth_year}, wrote <em>{$book_title}</em> and said: \"{$book_quote}\".</p>";
}
?>

That’s it!

Now, when you create a “Quote” post and link it to a “Book” and “Author,” the template will pull the content from both and display it.

techhipbettruvabetnorabahisbahis forumutaraftarium24eduedusedueduedueduedusedueduedus