admin管理员组

文章数量:1122846

I'm already able to :

  1. Create two different Custom Post Types (for example: "Books" and "Authors") and display their content.
  2. Link these two Custom Post Types together in back-office and front-office (using WP Query and "relationship field" from ACF) ;
  3. Display data from the first CPT into the second one (for example, a "Book post" displays informations about the "Author post", and an "Author post" displays informations about a "Book post").

What I want to achieve is to generate programmatically on a third post (neither a book post nor an author post) a specific content which merges data from both posts. Let's say in order to illustrate this point :

"John Doe born in 1900 who wrote Title of the novel is the author of this famous quote : [here the quote].

The above example is different than displaying content from "author post" on "book post". Its aim is precisely generatinf this content on a third post which is neither example/john-doe/ nor example/title-of-the-novel/, but example/quote-from-john-doe-in-title-of-the-novel/.

Is there a wordpress function that I can use to generate new posts from content extracted on others posts ?

I'm already able to :

  1. Create two different Custom Post Types (for example: "Books" and "Authors") and display their content.
  2. Link these two Custom Post Types together in back-office and front-office (using WP Query and "relationship field" from ACF) ;
  3. Display data from the first CPT into the second one (for example, a "Book post" displays informations about the "Author post", and an "Author post" displays informations about a "Book post").

What I want to achieve is to generate programmatically on a third post (neither a book post nor an author post) a specific content which merges data from both posts. Let's say in order to illustrate this point :

"John Doe born in 1900 who wrote Title of the novel is the author of this famous quote : [here the quote].

The above example is different than displaying content from "author post" on "book post". Its aim is precisely generatinf this content on a third post which is neither example.com/john-doe/ nor example.com/title-of-the-novel/, but example.com/quote-from-john-doe-in-title-of-the-novel/.

Is there a wordpress function that I can use to generate new posts from content extracted on others posts ?

Share Improve this question edited Sep 29, 2024 at 14:03 PhpDoe asked Sep 28, 2024 at 10:33 PhpDoePhpDoe 2992 silver badges11 bronze badges 1
  • Might be useful to add when you want that to happen, and if it needs to be updated when things change. E.g. A one-time generation triggered from admin, or the new post type is updated any time either source type changes. And, if there isn't any other way to know it, how will the programmatic generation of the third post type know which two Author and Book types to use to generate each new type? Will they be linked somehow, or some field in them the programmatic code will use? Is this one to one, or many to one? (E.g. one Author may have many books - should that be many new Book-Author posts?) – mozboz Commented Sep 30, 2024 at 9:52
Add a comment  | 

2 Answers 2

Reset to default 1

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.

You can use wp_insert_post( array $postarr, bool $wp_error = false, bool $fire_after_hooks = true ): int|WP_Error to programmatically insert new posts in WordPress. See the function documentation for details about the parameters.

For example you could create the following helper functions to get the job done.

function wpse_427001_get_book_data( int $post_id ): array {
    return array(
        'title' => '', // e.g. from some meta field or post_title
        'quote' => '', // e.g. from some meta field
    );
}

function wpse_427001_get_author_data( int $post_id ): array {
    return array(
        'name' => '', // e.g. from some meta field,
        'date_of_birth' => '', // e.g. from some meta field,
    );
}

function wpse_427001_generate_third_post( int $book_id, int $author_id ): int {
    $book = wpse_427001_get_book_data( $book_id );
    $author = wpse_427001_get_author_data( $author_id );

    // check book and author data exists to avoid creating posts from empty data...

    $post_id = wp_insert_post( array(
        'post_status' => 'publish',
        'post_type' => 'my_post_type',
        'post_title' => sprintf(
            'Quote from %s',
            esc_html( $author['name'] )
        ),
        'post_content' => sprintf(
            '%1$s born in %2$s who wrote %3$s is the author of this famous quote : %4$s',
            esc_html( $author['name'] ),
            esc_html( $author['date_of_birth'] ),
            esc_html( $book['title'] ),
            esc_html( $book['quote'] ),
        ),
    ) );

    return is_int( $post_id ) ? $post_id : 0;
}

N.B Demonstration purposes only. Update to match your exact use case.

The wpse_427001_generate_third_post() can then be used on suitable action to generate the post after getting the required author and book ids from somewhere.

本文标签: