WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Type Query (4 posts)

  1. staceyzav
    Member
    Posted 2 years ago #

    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!

  2. staceyzav
    Member
    Posted 2 years ago #

    I'm really close here - I now have the custom post type query working but can't get the category/taxonomy within the post-type to work

    <ul class="posts">
         <?php
         $query = new WP_Query(array('post_type' => 'listing', 'Category' => get_the_term_list( $post->ID, '60' )));
         while ($query->have_posts()) : $query->the_post();
         ?>
    
      <li>
         <div class="related-thumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('medium'); ?></a></div>
         <h2><a href=" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
         <h4><?php echo get_post_meta($post->ID, "Description", true); ?></h4>
         <div class="clear"></div>
      </li>
    
        <?php endwhile; wp_reset_query(); ?>
     </ul>

    It is just pulling all the posts from the Listings Post type.

  3. staceyzav
    Member
    Posted 2 years ago #

    Well, I figured it out! For those of you with the same type of question: THIS WORKS!!!

    <?php
      query_posts( array( 'post_type' => 'listing', 'listing_category' => 'acupuncture-naturopathic' ) );
      if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>
    
      <h3><?php the_title(); ?></h3>
      <?php the_content(); ?>
    
    <?php endwhile; endif; wp_reset_query(); ?>
  4. deepbevel
    Member
    Posted 2 years ago #

    nice, I was just working on the same and arrived at similar. I also discoverd how to output the category link. Just have to change to your taxonomy:

    <?php
    //list terms in a given taxonomy (useful as a widget for twentyten)
    $taxonomy = 'productscat';
    $tax_terms = get_terms($taxonomy);
    ?>
    <ul>
    <?php
    foreach ($tax_terms as $tax_term) {
    echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
    }
    ?>
    </ul>

    seems a lot for what is ussually just the_category..

Topic Closed

This topic has been closed to new replies.

About this Topic