Custom Post Type Query
-
I am modifying a theme that query’s post categories that you choose from the wordpress dashboard. I have added a custom post type and I want to pull in a specific category from the custom post type instead. I would like to hard-code this category into the theme file (instead of making it editable from the wordpress dashboard as it is now). I just need to figure out the query for the custom post type. post types are named “Listings” and I want to be able to pull in 6 posts from a category within the custom post type with the either the slug or category id.
Here is my modified functions code that created the custom post types:
/** Listings */ add_action('init', 'listing_register'); function listing_register() { $labels = array( 'name' => _x('Listings', 'post type general name'), 'singular_name' => _x('listing Item', 'post type singular name'), 'add_new' => _x('Add New', 'listing item'), 'add_new_item' => __('Add New listing Item'), 'edit_item' => __('Edit listing Item'), 'new_item' => __('New listing Item'), 'view_item' => __('View listing Item'), 'search_items' => __('Search listing'), 'not_found' => __('Nothing found'), 'not_found_in_trash' => __('Nothing found in Trash'), 'parent_item_colon' => '' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'menu_icon' => get_stylesheet_directory_uri() . '/article16.png', 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title','editor','thumbnail') ); register_post_type( 'listing' , $args ); } register_taxonomy("listing_category", array("listing"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => true));Here is the code from the theme file that currently pulls in categories from a regular post:
<?php $cc = 0; $c = 10; $posts_num = option::get('featured_categories_posts'); while ($cc < $c) { $cc++; $category = option::get('featured_category_'.$cc); if ($category != 0) { $cat = get_category($$category,false); $catlink = get_category_link($$category); $loop = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ), 'posts_per_page' => $posts_num, 'orderby' => 'date', 'order' => 'DESC', 'cat' => $category ) ); ?> <div id="tab1" class="tab_content"> <?php if ( $loop->have_posts() ) : ?> <ul class="posts"> <?php $x = 0; while ($loop->have_posts()) : $loop->the_post(); update_post_caches($posts); $x++; ?> <li<?php if ($x == 6) {echo ' class="last"';} ?>> <div class="cover"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><img src="<?php echo $image; ?>" alt="<?php the_title_attribute(); ?>" /></a> </div> <h2><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> </li><?php endwhile; ?> </ul><?php endif; ?> </div><!-- end .tab_content --> <?php } // if category is set } // endwhile ?>Thanks in advance!
The topic ‘Custom Post Type Query’ is closed to new replies.