Couple things here.
1. Your query needs to be inside the shortcode call back function to have access to the $args.
2. The shortcoce callback function needs to return a value and can’t print or echo anything. You can use output buffering to catch the output and store it in a returnable variable.
3. For your arguments you need to get rid of uppercase characters and spaces. This also goes for your meta_keys and meta_values.
4. Use wp_reset_postdata instead of wp_reset_query.
Here is a sample of how this would work:
function pos_listings( $atts ) {
extract( shortcode_atts( array(
'school' => 'bghs',
'cluster' => 'finance',
), $atts
)
);
$args = array(
'post_type' => 'programs',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'school',
'value' => $school,
'compare' => '='
),
array(
'key' => 'cluster',
'value' => $cluster,
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
ob_start() ?>
<?php if ( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_postdata();
$return = ob_get_clean();
return $return;
}
add_shortcode( 'pos_list', 'pos_listings' );
Thanks for your response. Couple questions before I jump in. Doesn’t this example in the Codex use upper case and spaces for the Key? http://codex.ww.wp.xz.cn/Custom_Fields
I am very new to working with custom fields so I want to make sure I understand what your sharing with me.
Also as I was working when I would manually add the $school and $cluster content into the $args = array (instead of pulling those variable from my shortcode that I could not get to work) it would filter my posts like I wanted and as I have a lot of content to filter using the shortcode seemed the most efficient way to accomplish this.
Please let me know about the lowercase and spaces and any other advise or help is greatly appreciated.
Thank You!
I’ve tried to study and implement you help. My Page has the following shortcode and it ignores it
[pos_list school=”BGHS” cluster=”Architecture & Construction”]
but if I replace
‘value’ => $school,
‘value’ => $cluster,
with
‘value’ => ‘BGHS’,
‘value’ => ‘Architecture & Construction’,
it works perfectly. How do I get my variables from my shortcode to read into the function. Scratching my head? Started reading about the output buffering to catch them but to be honest I don’t really understand it completely.
<?php
/**
* POS Short Code
**/
function pos_listings($atts) {
extract(shortcode_atts( array(
'school' => '',
'cluster' => '',
), $atts
)
);
$args = array(
'post_type' => 'programs',
'posts_per_page' => -1,
'orderby'=> 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'school',
'value' => $school,
'compare' => '='
),
array(
'key' => 'cluster',
'value' => $cluster,
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
ob_start() ?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_postdata();
$return = ob_get_clean();
return $return;
}
add_shortcode( 'pos_list', 'pos_listings' );
?>