Home › Forums › General Issues › Custom Shortcode + Querying and Ordering Posts
Working on a custom shortcode function inside functions.php that would display a ‘speakers’ post-type, with ‘featured_speaker’ ACF-checkbox and in a specific order set by ACF select-element ‘speaker_order’. But the custom shortcode does not display anything. I’ve checked the code many times but I can’t spot what I’m doing incorrectly.
add_shortcode( 'featured_speakers', 'custom_speakers' );
function custom_speakers() {
$args = array(
'post_type' => 'speaker',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'featured_speaker',
'value' => '1',
'compare' => '=',
),
array(
'key' => 'speaker_order',
'orderby' => 'meta_value',
'order' => DESC,
),
),
);
$query = new WP_Query( $args );
$out = '';
$out .= '<div class="full_section_inner">';
if ( $query->have_posts() ) {
// The Loop
while ( $query->have_posts() ) {
$out .= '<div class="vc_col-sm-3">';
$out .= '<div class="wpb_single_image">';
$out .= '<a href="'.get_field('speaker_link').'" target="_blank">';
$out .= '<div class="vc_single_image-wrapper vc_box_border_circle vc_box_border_grey">';
//$out .= '<img width="150" height="150" src="http://growcommerce.co/wp-content/uploads/2016/06/0c02f46-150x150.jpg" class="vc_single_image-img attachment-thumbnail" alt="Miki Beradelli, CEO, Kidbox" srcset="http://growcommerce.co/wp-content/uploads/2016/06/0c02f46-150x150.jpg 150w, http://growcommerce.co/wp-content/uploads/2016/06/0c02f46.jpg 296w" sizes="(max-width: 150px) 100vw, 150px">';
$out .= get_the_post_thumbnail( get_the_ID(), 'thumbnail' );
$out .= '</div>';
$out .= '</a>';
$out .= '</div>';
$out .= '<div class="separator transparent"></div>';
$out .= '<div class="wpb_text_column wpb_content_element ">';
$out .= '<div class="wpb_wrapper">';
$out .= '<p style="text-align: center;">';
$out .= '<a href="https://www.linkedin.com/in/miki-racine-berardelli-b0022b3/" target="_blank">';
$out .= '<span style="color: #ffffff;">'.get_the_title().'</span>';
$out .= '</a>';
$out .= '
';
$out .= '<p style="text-align: center;">';
$out .= '<a href="'.get_field('speaker_link').'" target="_blank">';
$out .= '<span style="color: #ffffff;">'.get_field('company').'</span>';
$out .= '</a>';
$out .= '
';
$out .= '</div>';
$out .= '</div>';
$out .= '<div class="separator transparent"></div>';
$out .= '</div>';
}
wp_reset_postdata();
}
$out .= '</div>';
return $out;
}
You’re missing 2 things. Since your running this inside a function you need to declare the global $post variable.
function custom_speakers() {
global $post;
The second is that you’re not calling the_post()
while ( $query->have_posts() ) {
$query->the_post();
The topic ‘Custom Shortcode + Querying and Ordering Posts’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.