• Resolved unwrittendevin

    (@unwrittendevin)


    Hi,

    I’m having an issue with custom post types and sorting through categorized posts using WP_Query.

    The idea is that I want to populate different pages based on 5 custom post types I’ve created. They all use the same categories to organize the posts. I’d like to have the Category title display in an H2, then display all posts within that particular category.

    This is what I have so far

    <?php
         global $post;
    	$slug = $post->post_name;
    	$args = array(
    	'post_type'=> $slug
    	);
    	$the_query = new WP_Query( $args );
    
    	$categories = get_categories();
    	foreach($categories as $category) {
    	$myID = $category->ID;
    	$myCat = $category->name;
    	echo '<h2 class="clearfix">';
    	echo $myCat;
    	echo '</h2>';
    	echo '<div class="row-fluid">';
    	$new_args = array(
    	'post_type'=> $slug,
    	'category_name '=> $myCat,
    	'cat' => $myID
    	);
    	$my_query = new WP_Query( $new_args );
    	if ( $my_query->have_posts() ): while ( $my_query->have_posts() ) : $my_query->the_post();
    	echo '<div class="span2">';
    	$pdf_value = get_post_custom_values('item-location');
            foreach ( $pdf_value as $key => $value ) :
            echo '<a href="'.$value.'" target="_blank"><div class="thumbnail">';
    	endforeach;
    	the_post_thumbnail('full');
    	echo '</div><p>';
    	the_title();
    	echo '</p></a></div>';
    	endwhile;
    	endif;
    	wp_reset_postdata();
    	echo '</div>';
    	};
        wp_reset_postdata(); ?>

    What it does is display the category names, but then displays all the posts inside, regardless of categorization. The link to the demo page is

    Any help would be greatly appreciated!

    Devin

Viewing 2 replies - 1 through 2 (of 2 total)
  • 1. What is your custom post type? How you are getting custom post by following code:

    $slug = $post->post_name;

    2. You don’t need to write the following code:

    $args = array(
     'post_type'=> $slug
    );
    $the_query = new WP_Query( $args );

    3. Comment your code and try the following code:

    $args=array(
       'orderby' => 'name',
       'order' => 'ASC'
     );
      $categories=get_categories($args);
      foreach($categories as $category) {
        echo '<h2 class="clearfix">'. $category->name .'</h2>';
        $args = array(
    	'post_type'=> //Write the custom post type,
    	'cat' => $category->cat_ID,
            'post_status' => 'publish'
        );
        $the_query = new WP_Query( $args );
        if($the_query->have_posts()){
         echo '<ul>';
          while ( $the_query->have_posts() ) {
            $the_query->the_post();
    	echo '<li>';
    	 the_title();
    	echo '</li>';
          }
          echo '</ul>';
        wp_reset_postdata();
      }

    I hope this will work for you.

    Thread Starter unwrittendevin

    (@unwrittendevin)

    My Custom Post Type names are the same as the page slugs I am loading the pages on, so the page Illusions Vinyl Railings has a slug of “illusions-railing”, which matches the post type.

    Your code worked great! I was able to use it and just add the slug variable back in to differentiate the post type for the different pages I am applying this template to.

    thanks for the help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Post Types, WP_Query & Categories’ is closed to new replies.