custom post types - CPTs relationship

admin2025-04-18  0

I have two CPT - project and publication. Is there a way to "connect" those CPSs to the permalink looks like:

foo/project/lorem/publication/ -> archive-publication.php foo/project/lorem/publication/ipsum -> single-publication.php

So one project can have X publications. Something like CRUD logic but just the "Read" part (all publications and a single one).

My functions.php:

function publications_init() {

  $labels = array(
    'name'                => _x( 'Publications', 'Post Type General Name', 'cornell' ),
    'singular_name'       => _x( 'Publication', 'Post Type Singular Name', 'cornell' ),
    'menu_name'           => __( 'Publications', 'cornell' ),
    'parent_item_colon'   => __( 'Parent Publication', 'cornell' ),
    'all_items'           => __( 'All Publications', 'cornell' ),
    'view_item'           => __( 'View Publication', 'cornell' ),
    'add_new_item'        => __( 'Add New Publication', 'cornell' ),
    'add_new'             => __( 'Add New', 'cornell' ),
    'edit_item'           => __( 'Edit Publication', 'cornell' ),
    'update_item'         => __( 'Update Publication', 'cornell' ),
    'search_items'        => __( 'Search Publication', 'cornell' ),
    'not_found'           => __( 'Not Found', 'cornell' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'cornell' )
  );

  $args = array(
    'label'               => __( 'publications', 'cornell' ),
    'description'         => __( 'Publication news and reviews', 'cornell' ),
    'labels'              => $labels,
    // Features this CPT supports in Post Editor
    'supports'            => array( 'title', 'editor', 'thumbnail', /* 'comments', 'revisions', 'custom-fields', 'excerpt', 'author' */),
    // You can associate this CPT with a taxonomy or custom taxonomy. 
    // 'taxonomies'          => array( 'genres' ),
    /* A hierarchical CPT is like Pages and can have
    * Parent and child items. A non-hierarchical CPT
    * is like Posts.
    */ 
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'page'
  );

  register_post_type( 'publication', $args );
}

add_action( 'init', 'publications_init', 0 );

function projects_init() {

  $labels = array(
    'name'                => _x( 'Projects', 'Post Type General Name', 'cornell' ),
    'singular_name'       => _x( 'Project', 'Post Type Singular Name', 'cornell' ),
    'menu_name'           => __( 'Projects', 'cornell' ),
    'parent_item_colon'   => __( 'Parent Project', 'cornell' ),
    'all_items'           => __( 'All Projects', 'cornell' ),
    'view_item'           => __( 'View Project', 'cornell' ),
    'add_new_item'        => __( 'Add New Project', 'cornell' ),
    'add_new'             => __( 'Add New', 'cornell' ),
    'edit_item'           => __( 'Edit Project', 'cornell' ),
    'update_item'         => __( 'Update Project', 'cornell' ),
    'search_items'        => __( 'Search Project', 'cornell' ),
    'not_found'           => __( 'Not Found', 'cornell' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'cornell' )
  );

  $args = array(
    'label'               => __( 'projects', 'cornell' ),
    'description'         => __( 'Project news and reviews', 'cornell' ),
    'labels'              => $labels,
    // Features this CPT supports in Post Editor
    'supports'            => array( 'title', 'editor', 'thumbnail', 'excerpt' /* 'comments', 'revisions', 'custom-fields', 'author' */),
    // You can associate this CPT with a taxonomy or custom taxonomy. 
    // 'taxonomies'          => array( 'genres' ),
    /* A hierarchical CPT is like Pages and can have
    * Parent and child items. A non-hierarchical CPT
    * is like Posts.
    */ 
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'page'
  );

  register_post_type( 'project', $args );
}

add_action( 'init', 'projects_init', 0 );

I see that ACF has Post Object so maybe somehow with that?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744970785a277472.html

最新回复(0)