Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display Array from custom content type in single.php WordPress

  • REFUNDED

<strong>I have a custom content type called a song. Within this content type there are some meta fields created using http://www.deluxeblogtips.com/meta-box/</strong>

add_action( 'init', 'create_song' );
function create_song() {
$labels = array(
'name' => _x('Song', 'post type general name'),
'singular_name' => _x('Song', 'post type singular name'),
'add_new' => _x('Add New', 'Song'),
'add_new_item' => __('Add Song'),
'edit_item' => __('Edit Song'),
'new_item' => __('New Song'),
'view_item' => __('View Song'),
'search_items' => __('Search Item'),
'not_found' => __('No items found'),
'not_found_in_trash' => __('No items found in Trash'),
'parent_item_colon' => '',
);

$supports = array('title');

register_post_type( 'Song',
array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'supports' => $supports,
'rewrite' => array('slug' => 'song'),
'taxonomies' => array('category', 'post_tag'),
'query_var' => true,
'menu_position' => 45,
)
);
}


meta_song_name
meta_song_artist
meta_song_description
meta_song_embed</strong>

// Mix Fields
$meta_boxes[] = array(
'id' => 'song',
'title' => 'Song information',
'pages' => array( 'song'),
'fields' => array(


array(
'name' => 'Song name',
'id' => $prefix .'song_name',
'clone' => false,
'type' => 'text',
// Default value (optional)
'std' => ''
),
array(
'name' => 'Song artist',
'id' => $prefix .'song_artist',
'clone' => false,
'type' => 'text',
// Default value (optional)
'std' => ''
),
array(
'name' => 'Song description',
'id' => $prefix .'song_description',
'clone' => false,
'type' => 'textarea',
// Default value (optional)
'std' => ''
),
// WYSIWYG/RICH TEXT EDITOR
array(
'name' => __( 'Song Embed', 'rwmb' ),
'id' => "{$prefix}song_embed",
'type' => 'wysiwyg',
// Set the 'raw' parameter to TRUE to prevent data being passed through wpautop() on save
'raw' => true,
// Editor settings, see wp_editor() function: look4wp.com/wp_editor
'options' => array(
'textarea_rows' => 20,
'teeny' => true,
'media_buttons' => false,
'tinymce' => false

),
),
)
);


http://grfk.co.nz/nYTj

<strong>Once I have entered a song, I want to associate multiple songs with a post.

I use a meta box to do this as well, in this instance a cloneable dropdown</strong>

// Mix Fields
$meta_boxes[] = array(
'id' => 'mix',
'title' => 'Mix information',
'pages' => array( 'post'),
'fields' => array(

// POST
array(
'name' => __( 'Songs', 'rwmb' ),
'id' => "{$prefix}song",
'type' => 'post',
'clone' => 'true',

// Post type
'post_type' => 'song',
// Field type, either 'select' or 'select_advanced' (default)
'field_type' => 'select_advanced',
// Query arguments (optional). No settings means get all published posts
'query_args' => array(
'post_status' => 'publish',
'posts_per_page' => '-1',
)
),
)
);


http://grfk.co.nz/gZeO

My problem is displaying this information in my single.php.

<?php $meta_song = get_post_meta( get_the_ID(), 'meta_song', true ); ?><?php echo $meta_song?>

This just returns 'Array'.

I am familiar with the helper class that comes with the metabox plugin, but I can't seem to get anywhere with displaying this data in a post.



Answers (3)

2013-08-07

Vinod Dalvi answers:

To echo array content use print_r() function in place of echo as shown in the following code

<?php $meta_song = get_post_meta( get_the_ID(), 'meta_song', true ); ?><?php print_r($meta_song); ?>


Nick comments:

Hi Vinod, that produces:

Array ( [0] => 176 [1] => 174 [2] => 176 )

Which is the ID's of the song posts but not the content of each song post.

http://grfk.co.nz/vrgJ


Vinod Dalvi comments:

You can write the following codeto display songs information :


// The Query
args = array('posts_per_page' => -1, 'post_type' => 'song');
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo 'meta_song_name '.get_post_meta( get_the_ID(), 'meta_song_name', true ).' <br />';
echo 'meta_song_artist'.get_post_meta( get_the_ID(), 'meta_song_artist', true ).' <br />';
echo 'meta_song_description'.get_post_meta( get_the_ID(), 'meta_song_description', true ).' <br />';
echo 'meta_song_embed'.get_post_meta( get_the_ID(), 'meta_song_embed', true ).' <br />';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();


Vinod Dalvi comments:

Or you can also use the plugin function rwmb_meta() to display these values as shown in the following code :


// The Query

args = array('posts_per_page' => -1, 'post_type' => 'song');

$query = new WP_Query( $args );



// The Loop

if ( $query->have_posts() ) {

while ( $query->have_posts() ) {

$query->the_post();

echo 'meta_song_name '.rwmb_meta( 'meta_song_name').' <br />';

echo 'meta_song_artist'.rwmb_meta( 'meta_song_artist' ).' <br />';

echo 'meta_song_description'.rwmb_meta('meta_song_description' ).' <br />';

echo 'meta_song_embed'.rwmb_meta( 'meta_song_embed' ).' <br />';

}

} else {

// no posts found

}

/* Restore original Post Data */

wp_reset_postdata();


Vinod Dalvi comments:

Or you can also use the plugin function rwmb_meta() to display these values as shown in the following code :


// The Query

args = array('posts_per_page' => -1, 'post_type' => 'song');

$query = new WP_Query( $args );



// The Loop

if ( $query->have_posts() ) {

while ( $query->have_posts() ) {

$query->the_post();

echo 'meta_song_name '.rwmb_meta( 'meta_song_name').' <br />';

echo 'meta_song_artist'.rwmb_meta( 'meta_song_artist' ).' <br />';

echo 'meta_song_description'.rwmb_meta('meta_song_description' ).' <br />';

echo 'meta_song_embed'.rwmb_meta( 'meta_song_embed' ).' <br />';

}

} else {

// no posts found

}

/* Restore original Post Data */

wp_reset_postdata();


Nick comments:

Can you please add the PHP tags to your code?


Vinod Dalvi comments:

Here it is with PHP tag :

<?php
// The Query
args = array('posts_per_page' => -1, 'post_type' => 'song');
$query = new WP_Query( $args );


// The Loop
if ( $query->have_posts() ) {

while ( $query->have_posts() ) {

$query->the_post();

echo 'meta_song_name '.rwmb_meta( 'meta_song_name').' <br />';
echo 'meta_song_artist'.rwmb_meta( 'meta_song_artist' ).' <br />';
echo 'meta_song_description'.rwmb_meta('meta_song_description' ).' <br />';
echo 'meta_song_embed'.rwmb_meta( 'meta_song_embed' ).' <br />';
}
} else {

// no posts found
}

/* Restore original Post Data */
wp_reset_postdata();
?>


If you are facing any issue doing this then contact me on mail id [email protected]
I will do the necessary changes in your code.


Nick comments:

Sent you an email


Nick comments:

Hi Vindo,

That code displays all songs on the page, not the songs that are assigned to the post.

Any ideas?


Vinod Dalvi comments:

I Have sent you an email that contains updated code to display songs attached to the post.


Nick comments:

Hello Vinod, unfortunately that code does not work. I have requested a refund from the website, thanks for your efforts.

2013-08-07

Sabby Sam answers:

Yes,
Exactly you have to use print_r to display the array.
print_r($meta_song);

please visit the below link
http://php.net/manual/en/function.print-r.php


Sabby Sam comments:

This code alternate help you to reterive the post <?php

$the_query = new WP_Query( array( 'taxonomy' => 'Song','term' => 'your-term' ) );

// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<ul>' ;
echo '<li> <a href="'.get_permalink().'">' . get_the_title() . '</a></li>';
echo '</ul>' ;
}
}
// run query
wp_reset_postdata(); ?>

2013-08-10

Liam Bailey answers:

Hi,

I dont want to steal anyone's thunder here, as I know someone said they emailed an answer or something but to get the songs attached to the post output in your single.php here is what I would do:

<?php

$meta_songs = get_post_meta( get_the_ID(), 'meta_song', true );

if (is_array($meta_songs) && !empty($meta_songs)) {
?><h2>Songs on the Mix</h2>
<ul><?php
foreach($meta_songs as $attached_song) {
if ($song_post = get_post($attached_song)) {
?><li><a href="<?php echo get_permalink($attached_song); ?>"><?php echo $song_post->post_title; ?></a></li>
}
}
?></ul>
}

?>