The string you are outputting using print_r()
is what is known as a serialized string. When WordPress stores an array of values in 1 custom field, it compresses the array into this string using a function called serialize()
What you want to do is to unserialize
the string, and then you can access each “associated index” in the array.
Usually get_post_meta()
unserializes the string for you, but it looks like the custom function GetSettings::getPostMeta()
does not. In this case we can run maybe_unserialize()
which will convert it into a usable array if it is able to.
Then we can simply loop through the array, outputting what you need to.
$aCoupon = GetSettings::getPostMeta($post->ID, 'coupon');
$aCoupon = maybe_unserialze( $aCoupon );
if ( empty( $aCoupon ) ){
foreach ( $aCoupon as $key => $value ) {
echo '<div><span class="label">' . $key . '</span> ' . $value . '</div>';
}
}
Based on your example, it would output something like this.
<div><span class="label">highlight</span> Test Highlight</div>
<div><span class="label">title</span> 50% off food</div>
<div><span class="label">description</span> Test description</div>
<div><span class="label">code</span> 1524521</div>
<div><span class="label">popup_image</span> 4548</div>
<div><span class="label">popup_description</span> Popup description</div>
<div><span class="label">redirect_to</span> </div>
Or if you want to access 1 value directly.
$aCoupon = GetSettings::getPostMeta($post->ID, 'coupon');
$aCoupon = maybe_unserialze( $aCoupon );
if ( empty( $aCoupon ) ){
//If the title is set, and is not empty, output it.
if ( isset( $aCoupon['title'] ) && '' !== $aCoupon['title'] ) {
echo '<div><span class="label">Title</span> ' . $aCoupon['title'] . '</div>';
}
}
EDIT by @Shaun21uk – I got it to work making some minor changes (full code below). For whatever reason the $aCoupon = GetSettings::getPostMeta($coupon_id, 'coupon');
was breaking the function – I still would like to know how to tap into the theme’s way of handling data.
Your shortcode function should look something like this:
add_shortcode( 'qg_shortcode', 'qg_shortcode' );
function qg_shortcode() {
$q = new WP_Query(array(
'post_type' => 'listing',
'posts_per_page' => 5,
'meta_key' => 'wc_coupon',
'meta_value' => ' ',
'meta_compare' => '!='
));
if ( $q->have_posts() ) :
while ( $q->have_posts() ) : $q->the_post();
echo '<h2 class="text-center"><a href="' . get_permalink() . '">';
the_title();
echo '</a></h2>';
the_content();
// Get the ID of the attached coupon.
$coupon_id = get_post_meta( get_the_ID(), 'wc_coupon', true );
// Get the coupon details from the coupon using the coupons ID.
$aCoupon = maybe_unserialize( $coupon_id );
// This checks if the coupon details are NOT empty, if that is true, then output the info.
if ( ! empty( $aCoupon ) ) {
// If the title is set, and is not empty, output it.
if ( isset( $aCoupon['title'] ) && '' !== $aCoupon['title'] ) {
echo '<div><span class="label">Title</span> ' . $aCoupon['title'] . '</div>';
}
}
echo'<div class="offer-button">';
echo'<a href="' . get_permalink() . '" class="offer-button-text">Claim this offer!</a>';
echo'</div>';
endwhile;
wp_reset_postdata();
endif;
}