Retrieve all custom field values of a specific custom field metakey as an array, inside WP_Query

Question

I’m building a function into a shortcode to retrieve information relative to a custom post. For the most part, I can retrieve everything I need with the following, despite (probably) not being the most elegant code and a bit long-winded.

function gazdept_shortcode($attr){
    $a = shortcode_atts( array(
        'posts' => 1,
        'issue' => '',
        'dept' => '',
        'authorpic' => '',
        'authorpic2' => '',
        'section' => ''
    ), $attr );
    $q = new WP_Query(array(
        'post_type' => 'gazette',
        'posts_per_page' => esc_attr($a['posts']),
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'gazissue',
                'field' => 'slug',
                'terms' => 'issue-'.esc_attr($a['issue'])
            ),
            array(
                'taxonomy' => 'gaztype',
                'field' => 'slug',
                'terms' => 'deptintro'
            ),
            array(
                'taxonomy' => 'gazdept',
                'field' => 'slug',
                'terms' => esc_attr($a['dept'])
            ),
        ),
    ));
    $content = '';
    
    
    
    $content .= '<p>';
        while($q->have_posts()) : $q->the_post();
        
        $author = get_post_meta( get_the_ID(), 'gazetteauthor', true);
        $jobtitle = get_post_meta( get_the_ID(), 'gazetteauthorpos', true);
        $email = get_post_meta( get_the_ID(), 'gazetteauthoremail', true);
        $emailph = (! empty($email)? '<a class="authoremail" href="mailto:' . $email . '">Email</a>' : '');
        $photo = esc_attr($a['authorpic']);
        $photoframe = (! empty($photo)? '<div class="profilepic" style="width:100px;height:100px;overflow:hidden;"><img style="max-width:100%;" src="'.$photo.'"></div>' : '');

        $author2 = get_post_meta( get_the_ID(), 'gazetteauthor2', true);
        $jobtitle2 = get_post_meta( get_the_ID(), 'gazetteauthor2pos', true);
        $email2 = get_post_meta( get_the_ID(), 'gazetteauthor2email', true);
        $emailph2 = (! empty($email2)? '<a class="authoremail" href="mailto:' . $email2 . '">Email</a>' : '');
        $photo2 = esc_attr($a['authorpic2']);
        $photoframe2 = (! empty($photo2)? '<div class="profilepic" style="width:100px;height:100px;overflow:hidden;"><img style="max-width:100%;" src="'.$photo2.'"></div>' : '');
    
        $links = get_post_meta( get_the_ID(), 'gazettelinks', true);
    
        $content .= get_the_content_with_formatting();
        $content .= '<table class="signofftable"><tbody><tr><td>';
        $content .= $photoframe;
        $content .= $author;
        $content .= '<br /><small>'.$jobtitle.'</small>';
        $content .= '<br />'.$emailph;
        $content .= '</td><td>';
        $content .= $photoframe2;
        $content .= $author2;
        $content .= '<br /><small>'.$jobtitle2.'</small>';
        $content .= '<br />'.$emailph2;
        $content .= '</td>';
        $content .= '</tr></tbody></table>';
        $content .= $links;
    endwhile;
   
    $content .= '</p>';
    wp_reset_query();
    return $content;
    
}
add_shortcode('gazdept', 'gazdept_shortcode');

The part where I’m having trouble are the following lines, because while this works to retrieve a single custom field value/metakey pair, any one post may have multiple values for the same metakey, stored in the database as separate records with different a meta_id in wp_postmeta.

$links = get_post_meta( get_the_ID(), 'gazettelinks', true);
...
$content .= $links;

I need to somehow retrieve all values for a particular meta-key within the query and output them as some kind of array. And that’s what I have no idea how to do. I’ve tried using the suggestions at the following link but to no avail https://wpsites.net/wordpress-tips/get-all-values-for-1-specific-custom-field-key/, and I’ve got no other ideas about how to achieve this.

Any advice would be very much appreciated.

0
Trev Wilcox 2 months 0 Answers 12 views 0

Leave an answer