Ask your WordPress questions! Pay money and get answers fast! (more info)

Custom Post Type and Category - Cant get to display posts WordPress

  • SOLVED

I have a custom post type 'property' and a file called 'taxonomy-propertycategory-under-contract-show.php'. It does pull up this file but it does not display the post that I have in that category. I even added 'cat' to the actual ID of this category and it still returns an empty set. I also left out the 'category_name' in args below and no go.

Wish I could show the website, but its a localhost site.


<?php echo "Recent Transactions - Under Contract Show:"; ?>
<?php global $post;

$args = array(
'post_type' => 'property',
'category_name' => 'under-contract-show',
'meta_key' => 'ecpt_date_list',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);

$my_query = new WP_Query( $args );
echo '<pre>';
print_r($args);
echo '</pre>';
if( $my_query->have_posts() ) {
while( $my_query->have_posts() ) {
$my_query->the_post();

Answers (1)

2013-01-05

Dbranes answers:

Hi, just to test the basic first, what does this give you?

<?php
$args = array('post_type' => 'property');

$my_query = new WP_Query($args);

echo "<h2>My property posts:</h2>";
echo "<ul>";
while ($my_query->have_posts()) : $my_query->the_post();
echo "<li>title:";
the_title();
echo "</li>";
endwhile;
echo "</ul>";
?>


69developer comments:

That displays all the properties.


Dbranes comments:

ok, so then we now the loop is working and the post_type is correct ;-)

What about this then?


<?php
$args = array(
'post_type' => 'property',
'category_name' => 'under-contract-show',
);

$my_query = new WP_Query($args);
echo "<h2>My property posts:</h2>";
echo "<ul>";
while ($my_query->have_posts()) : $my_query->the_post();
echo "<li>title:";
the_title();
echo "</li>";
endwhile;
echo "</ul>";
?>


69developer comments:

Here is my CPT code, I think the problem might be in here:

function register_property_posttype() {
$labels = array(
'name' => _x( 'Properties', 'post type general name' ),
'singular_name' => _x( 'Property', 'post type singular name' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Property' ),
'edit_item' => __( 'Edit Property' ),
'new_item' => __( 'Property' ),
'view_item' => __( 'View Property' ),
'search_items' => __( 'Search Property' ),
'not_found' => __( 'Property' ),
'not_found_in_trash'=> __( 'Property' ),
'parent_item_colon' => __( 'Property' ),
'menu_name' => __( 'Property' )
);

$taxonomies = array('propertycategory');

$supports = array('title','editor','thumbnail', 'excerpt');

$post_type_args = array(
'labels' => $labels,
'singular_label' => __('Property'),
'public' => true,
'show_ui' => true,
'publicly_queryable'=> true,
'query_var' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'property', 'with_front' => false ),
'supports' => $supports,
'menu_position' => 5,
'taxonomies' => $taxonomies
);
register_post_type('property',$post_type_args);
}
add_action('init', 'register_property_posttype');



// registration code for propertycategory taxonomy
function register_propertycategory_tax() {
$labels = array(
'name' => _x( 'Property Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Property Category', 'taxonomy singular name' ),
'add_new' => _x( 'Add New Property Category', 'Property Category'),
'add_new_item' => __( 'Add New Property Category' ),
'edit_item' => __( 'Edit Property Category' ),
'new_item' => __( 'New Property Category' ),
'view_item' => __( 'View Property Category' ),
'search_items' => __( 'Search Property Categories' ),
'not_found' => __( 'No Property Category found' ),
'not_found_in_trash' => __( 'No Property Category found in Trash' ),
);

$pages = array('property');

$args = array(
'labels' => $labels,
'singular_label' => __('Property Category'),
'public' => true,
'show_ui' => true,
'hierarchical' => true,
'query_var' => true,
'show_tagcloud' => false,
'show_in_nav_menus' => true,
'rewrite' => array('slug' => 'propertycategory', 'with_front' => false ),
);
register_taxonomy('propertycategory', $pages, $args);
}
add_action('init', 'register_propertycategory_tax');


69developer comments:

When I add the 'category' or 'cat', the loop returns nothing.


Dbranes comments:

ahh, ok try this $args array:

$args = array(
'post_type' => 'property',
'tax_query' => array(
array(
'taxonomy' => 'propertycategory',
'field' => 'slug',
'terms' => 'under-contract-show'
)
)
);


Dbranes comments:

and here is the updated $args with meta_key:

$args = array(
'post_type' => 'property',
'meta_key' => 'ecpt_date_list',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'propertycategory',
'field' => 'slug',
'terms' => 'under-contract-show'
)
)
);


69developer comments:

Forgot that I had to use 'taxonomy'....

That worked, thanks and I voted it to you.