• I’m trying to make a short code that prints out a glimpse of all the custom post types in a custom taxonomy. As far as I can make out it should work, but it doesn’t.
    I’m not a programmer and this has been put together trawling the web, forums and the codex.
    It could be I’ve made a spelling mistake or missed a comma – but I can’t find the mistake.

    function wg_album($wguse){
    	extract(shortcode_atts(
    		array('use' => 'work'), $wguse));
    	$args = array(
      			'post_type'   => 'work_gallery',
    			  'tax_query' => array(
    					array(
    						'taxonomy' => 'use',
    						'field' => 'slug',
    						'terms' => $wguse
    					)
    				)
    			);
    	$workpages = new WP_query($args);
    	ob_start(); ?>
    		<ul class="work-pages">
    	<?php foreach ($workpages as $wpage) { ?>
    
    			<li>
    				<a href="<?php echo get_page_link( $wpage->ID ); //the permalink?>">
    
    					<?php echo get_the_post_thumbnail($wpage->ID)//the featured image?>
    					<h3><?php echo $wpage -> post_title ; ?></h3>
    
    				<p> <?php echo $wpage -> post_excerpt ; ?></p>
    				</a>
    			</li>
    
    	<?php } ?>
    		</ul>
    <?php
    	return ob_get_clean();
    }

    Please help

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Your query is not going to retrieve various post types, only the ‘work_gallery’ post type. And only work_gallery posts that have certain terms in the ‘use’ taxonomy, not any terms like it sounds like you want.

    You actually can’t use WP_Query to determine what post types exists, it expects you to specify which post types you want. It’s going to take a raw mySQL query submitted using a $wpdb method to get what you want.

    Unfortunately I’ve no idea how to write such a query. All I know is you can’t get what you are looking for using WP_Query.

    I know you were hoping for someone to point out some sort of syntax error. If you define WP_DEBUG as true in wp-config.php, PHP will tell you where your syntax errors are. Sorry I can’t be of more help.

    Thread Starter nick_nielsen

    (@nick_nielsen)

    I obviously didn’t make myself clear…
    I want posts from the work-gallery post type that share the use taxonomy term passed by the variable $wguse.

    As it stands, neither the two components of the code – the passing of the variable from the shortcode and the query itself worked.

    I’ve since discovered that the first string in the shortcode_atts array becomes the variable in the code of the function – thus ‘use’ in the code becomes $use having taken the value offered up by $wguse which is transferred by the shortcode as it appears in the post. So that component now works.

    However the query doesn’t work – even after modifying the variable. On inspection, rather than getting the posts, I get multiple iterations of the permalink of the page which contains the shortcode.

    Thread Starter nick_nielsen

    (@nick_nielsen)

    Spelling mistake…
    always check your underscores and your hyphens…

    Thread Starter nick_nielsen

    (@nick_nielsen)

    It worked and then stopped working again and I haven’t changed anything since I got it to work.

    Don’t understand.

    Moderator bcworkz

    (@bcworkz)

    Apologies for my confusion. I’m not sure why I took what you said the way I did, what you intended by what you said makes much more sense!

    Anyway, sounds like your’re pretty close now. Sometimes browser caching can confuse your debugging attempts. It’s possible the working version is one iteration back and the current version has an issue, even though it appeared otherwise during your debugging session.

    If you haven’t already, define WP_DEBUG as true in wp-config.php. That can help track down errors. If you’re still stuck, post the current version of your code, maybe new eyes can spot the problem.

    PS – I know all too well about your hyphen and underscore mix up, I’ve been bitten by that one several times. Working with custom table columns is particularly rife with mixed character tags X/

    Thread Starter nick_nielsen

    (@nick_nielsen)

    I’ve got it working !
    In fact the question is why it started working at all.
    What I have discovered id that if you use WP_Query(), you have to set up a loop ( ‘if(has_posts()’ etc.) and you can’t use ‘foreach’ – my mistake was that hitherto I’ve used get_pages() or ‘get_posts()’ where it seems you can use ‘foreach’…
    So, the code now looks like this

    function wg_album($wguse){
    	extract(shortcode_atts(
    		array('use' => 'work'), $wguse));
    		$args = array(
      			'post_type'   => 'work-gallery',
      			'use' => $use,
      			'sort_column'  => 'menu_order'
    			);
    	$work_galleries = new WP_Query($args);
    	ob_start(); ?>
    		<ul class="work-pages">
    	<?php
    
      if ($work_galleries->have_posts()) {
    			while ($work_galleries->have_posts()) {
    				$work_galleries->the_post();?>
    			<li>
    				<a href="<?php the_permalink (); //the permalink?>">
    
    					<?php the_post_thumbnail()//the featured image?>
    					<h3><?php the_title() ; ?></h3>
    
    				<p> <?php the_excerpt() ; ?></p>
    				</a>
    			</li>
    	<?php }
    	} ?>
    		</ul>
    <?php
    	return ob_get_clean();
     }

    The question is now, why did it suddenly work when it shouldn’t have ?!

    Quite a learning curve !!

    Moderator bcworkz

    (@bcworkz)

    I think I see what’s going on now. I believe this explains it.

    It’s not so much that you cannot use foreach to run a loop, the key is the call to the_post() which does several “Loop” housekeeping tasks. Without that, some of the nifty template tags will not work right. Of course this is set up for a while loop, in some circumstances perhaps foreach would in fact not work, I’m not sure.

    I’m pretty sure foreach would work in some circumstances, but you need to keep in mind in this case you are working strictly with the data in the returned array, no nifty behind the scenes template tag management is going on. For instance, $wpage->post_excerpt simply contains the content of the associated DB column for the current post. Depending on how you do excerpts, it may be empty. But when you use the_excerpt() in the Loop, if the column value is an empty string, there is a filter callback hooked in that creates an excerpt from the the_content. My take away lesson from this would be: If used appropriately, foreach can work. When in doubt, use the “Loop”.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom post types, taxonomy in shortcode problems’ is closed to new replies.