Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to custom sort taxonomies on a custom query I have written for one of my website

I cannot use any custom plugin as I have tried and they do not work with my current setup.

add_action('init', 'ecommerce_create_post_type', 1);
function ecommerce_create_post_type() {

// Create new Latest-News custom post type
$labels = array(
'name' => _x('Products', 'post type general name'),
'singular_name' => _x('Products', 'post type singular name'),
'add_new' => _x('Add New', 'Product'),
'add_new_item' => __('Add New Product'),
'edit_item' => __('Edit Product'),
'new_item' => __('New Products'),
'view_item' => __('View Products'),
'search_items' => __('Search Products'),
'not_found' =>  __('No Products found'),
'not_found_in_trash' => __('No Products found in Trash'), 
'_builtin' =>  false, 
'parent_item_colon' => ''
  );
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true, 
'query_var' => true,
'sort' => true,
'register_meta_box_cb' => 'icom_meta_boxes',
'rewrite' => array(
        'slug' => 'products',
        'with_front' => false
    ),
'capability_type' => 'post',
'hierarchical' => true,
'menu_position' => 20,
'supports' => array('title','editor','thumbnail','custom-fields','excerpt'),
'menu_icon' => ICOMMERCE_DIR.'/images/cart.png'
  ); 
  register_post_type('products',$args);  

// Product categories, is heirarchical and can use permalinks
$labels = array(
        'name' => _x( 'Product Categories', 'taxonomy general name' ),
        'singular_name' => _x( 'Product Category', 'taxonomy singular name'),
        'search_items' => __( 'Search Categories'),
        'all_items' => __( 'All Product Categories'),
        'parent_item' => __( 'Product Category'),
        'parent_item_colon' => __( 'Parent Product Category:'),
        'edit_item' => __( 'Edit Product Category'),
        'update_item' => __( 'Update Product Category'),
        'add_new_item' => __( 'Add New Product Category'),
        'new_item_name' => __( 'New Product Category Name')
        );

register_taxonomy( 'icomcat', 'products', array(
    'rewrite' => array(
    'slug' => 'product-category',
    'with_front' => false,
    'sort' => true,
    ),
    'labels' => $labels,
) );
}

using the following code , I am fetching the latest products

  <?php
global $wpdb;
$prefix=$wpdb->prefix;
$latest_products = $wpdb->get_results(
  "SELECT a.term_id, a.name 
  FROM ".$prefix."terms a,".$prefix."term_taxonomy b 
  WHERE b.parent=".$prodcatid." 
  and a.term_id=b.term_id 
  and b.taxonomy='icomcat' 
  order by a.name desc"); 
if ( count( $latest_products ) > 0 ) {$count=0;
echo '<div class="clear3"> </div><div class="text12">  <h3> <a> <?php $current_category = single_cat_title("", false); ?></a></h3></div><div class="clear3"> </div><div id="cateogybox">';
    foreach ( $latest_products as $latest_product ) { $count=$count+1; 
?>

I am trying to sort these results using the date field. as you can see I am firing

$latest_products = $wpdb->get_results(
  "SELECT a.term_id, a.name 
  FROM ".$prefix."terms a,".$prefix."term_taxonomy b 
  WHERE b.parent=".$prodcatid." 
  and a.term_id=b.term_id 
  and b.taxonomy='icomcat' 
  order by a.name desc"); 

but my results are not sorted by date.

share|improve this question
    
What date field? Your queries are pulling from the terms and the term_taxonomy tables only and neither of those have a "date" field nor do you even attempt to ORDER BY such a field. –  s_ha_dum Mar 24 '13 at 18:01
add comment

2 Answers

$args = array(
   'post_type' => 'product',
    'tax_query' => array(
        array(
            'taxonomy' => 'icomcat',
            'field' => 'id',
            'terms' => array($prodcatid)
        )
    )
);
$postslist = get_posts( $args );
share|improve this answer
1  
Can you explain what this does and how it will sort by date ? I tried using it but no visible difference. –  Sam Apr 11 '12 at 7:28
    
Thanks Rajeev! This worked for me. –  ckpepper02 Mar 21 at 13:22
add comment

If you use the get_posts() function the default will be sort by date.

$latest_products = get_posts(array(
    'numberposts'     => -1,
    'post_type'       => 'products',
));

foreach($latest_products as $product) {
    print "{$product->post_title}<br>\n";
}

You also need to be aware that wordpress will automatically do a similar query when you put ?post_type=products in your url.

share|improve this answer
    
Only gets one post ? Also this broke the whole layout template. –  Sam Apr 11 '12 at 7:15
    
<?php $latest_products = get_posts(array( 'numberposts' => 1, 'taxonomy' => 'icomcat', 'post_type' => 'products', )); if ( count( $latest_products ) > 0 ) {$count=0; echo '<div class="clear3"> </div><div class="text12"> <h3> <a> <?php $current_category = single_cat_title("", false); ?></a></h3></div><div class="clear3"> </div><div id="cateogybox">'; foreach ( $latest_products as $latest_product ) { $count=$count+1; ?> <h2> <a><?php echo $latest_product->name; ?></a></h2> doesnt seem to work –  Sam Apr 11 '12 at 7:38
    
'numberposts'=>-1 (not 1) That means you get all posts. –  offroff Apr 11 '12 at 7:42
    
Sorry that was a typo. Even with -1 it is only giving me 1 post. –  Sam Apr 11 '12 at 7:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.