0

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' ); ?>" />
  • 1
    instead of saving the album title in a song, save the album ID. then the continuation will be easier. – Kaperto Nov 11 '19 at 19:33
  • It really did. This issue has been solved. Thank you very much for helping me. – name1996 Nov 12 '19 at 18:49
0

This issue has been solved by changing the code that displays posts a little.

<?php    
$albumsongs = get_posts( array('post_type' => 'song', 'numberposts' => -1, 'meta_key' => 'select_song_album', 'meta_value' => get_the_ID(),) );

foreach( $albumsongs as $post ){
setup_postdata($post);
?>
<div>
<span><?php the_title(); ?></span> <span><a href="<?php the_permalink(); ?>">View</a></span>
</div>
<?php
}
wp_reset_postdata();
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.