Maybe, the question is unclear for you, but I will try to explain.
There are two registered custom post types, song and album.
I have a metabox on a Create an Album, in which I can select one post from the Songs list. I will show you some pieces from that code placed in different functions:
// SELECTED
$values = get_post_custom( $post->id );
$selected = isset( $values['select_song_album'] ) ? esc_attr( $values['select_song_album'][0] ) : '';
// SELECT - OPTION
<select name="select_song_album" id="select_song_album">
<option value="Non-album">Non-album</option>
<?php
$song_album = array( 'post_type' => 'album', 'orderby' => 'title', 'order' => 'asc', );
$song_album_select = new WP_Query( $song_album );
while ( $song_album_select->have_posts() ) : $song_album_select->the_post();
?> <option value="<?php the_title(); ?>" <?php selected( $selected, get_the_title() ); ?> ><?php the_title(); ?></option>
<?php
endwhile;
?>
</select>
<input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />
// SAVE DATA
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post', $post_id ) ) return;
if( isset( $_POST['select_song_album'] ) )
update_post_meta( $post_id, 'select_song_album', esc_attr( $_POST['select_song_album'] ) );
So, on the Album template, I want to display all the songs selected on the page of editing/adding them. I tried this code, but it shows all the songs from that post type I have published:
<?php
$albums = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'album',
'post_status'=> 'publish',
'orderby' => 'title',
'order'=> 'ASC'
)
);
if ( $albums->have_posts() ) :
while ( $albums->have_posts() ) :
$albums->the_post();
endwhile;
endif;
wp_reset_query();
?>
<?php
$albumID = get_post_meta( get_the_ID(), 'select_song_album', true );
$songs = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'song',
'meta_key' => 'select_song_album',
'meta_value' => $albumID,
'orderby' => 'title',
'order'=> 'ASC'
)
);
?>
<?php if ( $songs->have_posts() ) : while ( $songs->have_posts() ) : $songs->the_post();
?>
<div>
<span><?php the_title(); ?></span>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
Could you show me how to display songs from an album? Thank you for the answer.
UPDATE: Changed the code of SELECT OPTION
// SELECT - OPTION
<select name="select_song_album" id="select_song_album">
<option value="0">Non-album</option>
<?php
$song_album = array( 'post_type' => 'album', 'orderby' => 'title', 'order' => 'asc', );
$song_album_select = new WP_Query( $song_album );
while ( $song_album_select->have_posts() ) : $song_album_select->the_post();
?> <option value="<?php the_ID(); ?>" <?php selected( $selected, get_the_ID() ); ?> ><?php the_title(); ?></option>
<?php
endwhile;
?>
</select>
<input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />