admin管理员组文章数量:1410717
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' ); ?>" />
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' ); ?>" />
Share
Improve this question
edited Nov 11, 2019 at 20:49
name1996
asked Nov 11, 2019 at 18:16
name1996name1996
13 bronze badges
2
- 1 instead of saving the album title in a song, save the album ID. then the continuation will be easier. – Kaperto Commented Nov 11, 2019 at 19:33
- It really did. This issue has been solved. Thank you very much for helping me. – name1996 Commented Nov 12, 2019 at 18:49
1 Answer
Reset to default 0This 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();
?>
本文标签: Show posts from WP Custom Post Type selected from a field in a metabox
版权声明:本文标题:Show posts from WP Custom Post Type selected from a field in a metabox 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745007539a2637351.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论