Question

Having the hardest time with my custom post types and retriving categories...

I have two custom post types that are registered and working so far: RV Rentals, and RV Sales.

I just need a template page that pulls in all the categories for RV sales, but I can't get this to work... the code seems to just be pulling in categories for RV Rentals, even though it should just be displaying ALL CPT categories for RV Rentals and RV Sales. Going insane trying to figure out how to have it ONLY pull in RV sales... can someone please help? I realize this is a lot of code I've posted but really don't know what is wrong here... losing my mind.

This is the functions file I have that registers my RV sales custom post type:

/* ------------------------------
CPT RV SALES  
------------------------------*/

add_action('init','create_rvsales_post_type'); 

function create_rvsales_post_type() {
$labels = array(
    'name' => 'RV Sales',
    'singular_name' => 'RV Sales',
);
$args = array(
    'labels' => $labels,
    'public' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_nav_menus' => false, 
    'show_in_menu' => true,
    'show_in_admin_bar' => true,
    'menu_position' => 20,
    'menu_icon' => null,
    'capability_type' => 'post',
    'hierarchical' => false,
    'supports' => array('title','thumbnail','editor'),
    'has_archive' => 'motorhomes-for-sale',
    'rewrite' => array('slug' => 'motorhomes-for-sale/%rvsales_cats%'),
    'query_var' => true,
    'can_export' => true,
); 
register_post_type('rv_sales',$args);
}

 //setup tax
 add_action( 'init', 'create_rvsales_taxonomies', 0 ); 


 function create_rvsales_taxonomies() 
 {
 $labels = array(
 'name' => _x( 'RV Sales Categories', 'taxonomy general name' ),
 'singular_name' => _x( 'RV Sales Category', 'taxonomy singular name' ),
  );

   register_taxonomy('rvsales_cats',array('rv_sales'), array(
  'hierarchical' => true,
  'labels' => $labels,
  'show_ui' => true,
  'query_var' => true,
  'rewrite' => array( 'slug' => 'motorhomes-for-sale' ),
    ));

 }

 register_taxonomy_for_object_type('category', 'rv_sales');

 //get that nice url structure
 function filter_post_type_link2($link, $post) {
 if (!in_array($post -> post_type, array(
         'rv_sales',
     )))
     return $link;


 if ($catsegors = get_the_terms($post -> ID, 'rvsales_cats')) {
    $link = str_replace('%rvsales_cats%', array_pop($catsegors) -> slug, $link); 
}

 return $link;
}

 add_filter('post_type_link2', 'filter_post_type_link2', 10, 2); 

Here's the template I have, that should be calling in ALL CPT categories (From RV SALES and RV Rentals), but it still just pulls in the categories from RV Rentals even when there's a bogus parameter in the 'rv_salesblah' for get_categories:

<?php 
                // get all the categories from the database
                    $cats = get_categories('rv_salesblah'); 
                    // loop through the categries
                    foreach ($cats as $cat) {

                    // setup the cateogory ID
                    $cat_id= $cat->term_id;

                    // Make a header for the category
                    echo "<h2>".$cat->name."</h2>";

                    // create a custom wordpress query
                    query_posts("cat=$cat_id&posts_per_page=100");

                    // start the wordpress loop!
                    if (have_posts()) : while (have_posts()) :     the_post();     ?>

                    <?php // create our link now that the post is setup ?>
                    <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
                    <?php echo '<hr/>'; ?>

                <?php endwhile; endif; // done our wordpress loop. Will start again for    each         category ?>
                <?php } // done the foreach statement ?>

EDIT: Fixed this, if some sorry soul comes across this same issue, this is the code I used to get me on the right track:

 <?php
 //for a given post type, return all
  $post_type = 'rv_sales';
$tax = 'rvsales_cats';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms  as $tax_term) {
 $args=array(
  'post_type' => $post_type,
  "$tax" => $tax_term->slug,
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1
);

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  echo 'List of  '. $tax_term->name;
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php     the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
   endwhile;
}
 wp_reset_query();
}
}
?>
Was it helpful?

Solution

EDIT: Fixed this, if some sorry soul comes across this same issue, this is the code I used to get me on the right track:

 ?php
 //for a given post type, return all
 $post_type = 'rv_sales';
$tax = 'rvsales_cats';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms  as $tax_term) {
 $args=array(
 'post_type' => $post_type,
  "$tax" => $tax_term->slug,
   'post_status' => 'publish',
  'posts_per_page' => -1,
   'caller_get_posts'=> 1
);

 $my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
 echo 'List of  '. $tax_term->name;
 while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php          the_title_attribute(); ?>"><?php the_title(); ?></a></p>
   <?php
  endwhile;
}
wp_reset_query();
}
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top