• I have a bunch of High Schools and Classes that I am trying to list/filter using a short code but it seems that my shortcode variable will not extend into my meta_query array. How do I pass a variable via the shortcode [pos_list school=”BGHS” cluster=”Architecture & Construction”] to filter from these 2 custom fields.

    <?php
    /**
    * Program of Study Short Code
    **/
    
    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' => '='
    			)
    
    		)
    
    	);
    
    }
    add_shortcode('pos_list', 'pos_listings');
    $the_query = new WP_Query( $args );
    ?>
    
    <?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_query();  // Restore global post data stomped by the_post(). ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • 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' );
    Thread Starter kokoruz

    (@kokoruz)

    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!

    Thread Starter kokoruz

    (@kokoruz)

    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' );
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Short Code Variable Not Being Read’ is closed to new replies.