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

Retrieve meta-key value of array and use it in shortocde WordPress

  • SOLVED

Hello, I am using Meta Box Plugin https://metabox.io/

Q: Field: Advanced Image field
clone= true

Problem: Retrieve the image ids and use it within gallery shortocde

[gallery ids="729,732,731,720"]

<?php echo do_shortcode('[gallery option1="value1"]'); ?>

Q2: If clone does not work with image field then retrieve the image ids from a single advanced image field and use it in shortocde.

I was able to retrieve it using foreach but couldn't make it work with shortocde.
https://metabox.io/docs/get-meta-value/
https://metabox.io/docs/cloning-fields/

I tied for single image filed but no luck
<?php
$retour= "";
$myposts = get_post_meta($post->ID, '_nc_pgallery1', false);
foreach($myposts as $post) :
$retour.= $post.',';
endforeach;
return $retour;
?>
<?php echo do_shortcode('[gallery ids="$retour"]'); ?>

Answers (2)

2015-11-09

Arnav Joy answers:

what this is returning ?

$myposts = get_post_meta($post->ID, '_nc_pgallery1', false);

print_r($myposts );


Jihan Ahmed comments:

the image filed has multiple images.

so it will be : get the value from a meta-key that has multiple values. (it will retun the ids of images) and use that value in gallery shortocde.

the above return Array ( )

2015-11-09

Rempty answers:

if is returning a empty array maybe $post-ID not exist and you are not inside a loop, inside a loop you can use $post->ID;

Inside a loop
if (have_posts()) : while (have_posts()) : the_post();
/*Here all the code*/
$myposts = get_post_meta($post->ID, '_nc_pgallery1', false);
//print_r($myposts);
foreach($myposts[0] as $post1) :
$retour[] =$post1[0];
endforeach;
$ids=implode(',',$retour);
echo do_shortcode('[gallery ids="'.$ids.'"]');

/**/
endwhile; endif;


Jihan Ahmed comments:

It returns the error http://www.paros.flyerworks.net/project/adopt-a-classroom-12/

Warning: Invalid argument supplied for foreach() in /home/markamia/public_html/paros/wp-content/themes/enfold-child/single-project.php on line 159

Warning: implode(): Invalid arguments passed in /home/markamia/public_html/paros/wp-content/themes/enfold-child/single-project.php on line 165


Also for ur reference https://perishablepress.com/wordpress-custom-fields-tips-tricks/

this returns the ids properly
<?php $songs = get_post_meta($post->ID, '_nc_pgallery1', false); ?>
<h3>This post inspired by:</h3>
<ul>
<?php foreach($songs as $song) {
echo '<li>'.$song.'</li>';
} ?>
</ul>


Rempty comments:

try this


if (have_posts()) : while (have_posts()) : the_post();

/*Here all the code*/

$myposts = get_post_meta($post->ID, '_nc_pgallery1', false);

//print_r($myposts);

foreach($myposts as $post1) :

$retour[] =$post1;

endforeach;

$ids=implode(',',$retour);

echo do_shortcode('[gallery ids="'.$ids.'"]');



/**/

endwhile; endif;



Jihan Ahmed comments:

hey it works.

Can u fix this for me I will increase the prize money to $10
I was able to list all post by category successfully http://www.paros.flyerworks.net/browse-by-category/
http://pastebin.ca/3245006

But I am facing problem to list it by year http://www.paros.flyerworks.net/browse-by-year/
http://pastebin.ca/3245001

I need to produce like
h2: year:2015
post1
post2
.
.
h2: year 2014
post1
post2


Rempty comments:

If you dont need pagination and just list all projects.

<?php
global $wpdb;
$cpt='project';
$sql = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' GROUP BY YEAR($wpdb->posts.post_date) ORDER BY $wpdb->posts.post_date DESC", $cpt);
$results = $wpdb->get_results($sql);
if ( $results )
{
foreach ($results as $r)
{
$year = date('Y', strtotime( $r->post_date ) );
?>
<h2><?php echo $year;?></h2>
<?php
$postbyyear=new WP_Query('post_type='.$cpt.'&year='.$year);
if($postbyyear->have_posts()):
?>
<ul>
<?php
while($postbyyear->have_posts()):
$postbyyear->the_post();
?>
<li><a href="<?php get_permalink()?>"><?php the_title()?></a></li>
<?php
endwhile;
?>
</ul>
<?php
endif;
}
}
?>


Jihan Ahmed comments:

this was solved earlier by Arnav .. I didn't get time to let u know the updates.