I just following this code from here https://wordpress.stackexchange.com/a/128627/142801
So basically I create 2 custom post type (cpt)
Series (for exp: Macgyver)
Movie (for exp: Macgyver eps 1, Macgyver eps 2 etc)
I write this code below and the list of series title show up on movie cpt as metabox
add_action( 'admin_init', 'add_meta_boxes' );
function add_meta_boxes() {
add_meta_box( 'some_metabox', 'Movies Relationship', 'movies_field', 'series' );
}
function movies_field() {
global $post;
$selected_movies = get_post_meta( $post->ID, '_movies', true );
$all_movies = get_posts( array(
'post_type' => 'movies',
'numberposts' => -1,
'orderby' => 'post_title',
'order' => 'ASC'
) );
?>
<input type="hidden" name="movies_nonce" value="<?php echo wp_create_nonce( basename( __FILE__ ) ); ?>" />
<table class="form-table">
<tr valign="top"><th scope="row">
<label for="movies">Movies</label></th>
<td><select multiple name="movies">
<?php foreach ( $all_movies as $movie ) : ?>
<option value="<?php echo $movie->ID; ?>"<?php echo (in_array( $movie->ID, $selected_movies )) ? ' selected="selected"' : ''; ?>><?php echo $movie->post_title; ?></option>
<?php endforeach; ?>
</select></td></tr>
</table>
}
add_action( 'save_post', 'save_movie_field' );
function save_movie_field( $post_id ) {
// only run this for series
if ( 'series' != get_post_type( $post_id ) )
return $post_id;
// verify nonce
if ( empty( $_POST['movies_nonce'] ) || !wp_verify_nonce( $_POST['movies_nonce'], basename( __FILE__ ) ) )
return $post_id;
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// check permissions
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
// save
update_post_meta( $post_id, '_movies', array_map( 'intval', $_POST['movies'] ) );
}
The question is how I can display this list of movie on the series posts (errr I bit confuse how to explain hope you guys understand) for example when I open
httpdomaindotcom/series/macgyver/
it will list all the movie eps of macgyver over there
I already try the other code in those thread above (about how to display the list) but not working
$series = new WP_Query( array(
'post_type' => 'movies',
'post__in' => get_post_meta( $series_id, '_movies', true ),
'nopaging' => true
) );
if ( $series-> have_posts() ) { while ( $series->have_posts() ) {
$series->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ></a></li>
<?php
} }
Anyone can help?
Thank you
Patty
movie
forseries
custom post type in your case. And the termMacgyver
can have all the episodes of the series and use tax query.var_dump( get_post_meta( $series_id, '_movies', true ) );
and see if it prints the correct "movies"/post IDs.