Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Shortcode for Custom Post Type WordPress

  • SOLVED

Hi I am having trouble with a shortcode that pulls category information from a Custom Post Type.

<strong>PART 1</strong>

<strong>/* Cities */</strong>
My custom post type:
register_taxonomy('citycat', 'city', array('hierarchical' => true, 'label' => __('City Categories'), 'singular_name' => 'Category'));
}
add_action('init', 'gr_post_type_city');

<strong>/* Shortcode */</strong> This shortcode gets all the information.
function gr_suburb($atts, $content = null) {
extract(shortcode_atts(array(
"numposts" => '5',
), $atts));
query_posts('post_type=city&showposts='.$numposts);


<strong>Whats needed</strong>
I require a query to that filters information from specific categories under "city".
EG: "sydney" is a category listed under "city"

Once I create "sydney" with a slug "sydney" as a category this is the data I get
wp-admin/edit-tags.php?action=edit&taxonomy=citycat&tag_ID=29&post_type=city

My attempts have been:

function gr_suburb($atts, $content = null) {
extract(shortcode_atts(array(
"numposts" => '5',
), $atts));
query_posts('post_type=city&tag_ID=29&showposts='.$numposts);


or

function gr_suburb($atts, $content = null) {
extract(shortcode_atts(array(
"numposts" => '5',
), $atts));
query_posts('post_type=city&cat=sydney&showposts='.$numposts);


But neither work, given in mind that there will also be several other categories along with Sydney including Melbourne, Darwin, Perth etc, so i only want query one at a time.

So what I am after is a query that just draws cities like &cities=sydney,melbourne

<strong>PART 2</strong>
When I create the shortcode instead of having multiple shortcodes in the PHP file I would like one snippet. So when I enter my my shortcode into the content area it looks like the following.

[suburb numposts="5" cityname="sydney"]

Answers (2)

2011-02-27

Ivaylo Draganov answers:

Hello,

you should use the new advanced taxonomy queries that come with WP 3.1. You can read more about them here:
[[LINK href="http://codex.wordpress.org/Function_Reference/query_posts#Taxonomy_Parameters"]]http://codex.wordpress.org/Function_Reference/query_posts#Taxonomy_Parameters[[/LINK]]
And some good examples here:
[[LINK href="http://ottopress.com/2010/wordpress-3-1-advanced-taxonomy-queries/"]]http://ottopress.com/2010/wordpress-3-1-advanced-taxonomy-queries/[[/LINK]]


Here's some code that I've put together, try it on:

<?php

function gr_suburb($atts, $content = null) {

extract(shortcode_atts(array(
'numposts' => '5', // number
'city' => '' // comma separated list of cities
), $atts));

$myquery = array(
'post_type' => 'city', // post type(s)
'posts_per_page' => intval($numposts), // convert string to integer
'tax_query' => array(
array(
'taxonomy' => 'citycat', // taxonomy
'terms' => explode(',', $city), // convert string to array
'field' => 'slug', // matching field
),
)
);

// start query
query_posts($myquery);

// using the values is up to you
print_r($myquery);

}

2011-02-27

Jermaine Oppong answers:

I improvised a shortcode similar to this last year on my blog:

[[LINK href="http://www.graphicbeacon.com/web-design-development/execute-multiple-custom-post-type-queries-using-wordpress-shortcodes/"]]Execute Multiple Custom Post Type Queries using WordPress Shortcodes[[/LINK]]