自定义查询的自定义分类排序

时间:2012-04-11 作者:Sam

我正在尝试对我为我的一个网站编写的自定义查询进行自定义分类

我无法使用任何自定义插件,因为我已经尝试过了,它们不适用于我当前的设置。

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,
) );
}
使用以下代码,我获取最新的产品

  <?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; 
?>
我正在尝试使用日期字段对这些结果进行排序。正如你所见,我正在开火

$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"); 
但我的结果不是按日期排序的。

2 个回复
最合适的回答,由SO网友:Rajeev Vyas 整理而成
$args = array(
   \'post_type\' => \'product\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'icomcat\',
            \'field\' => \'id\',
            \'terms\' => array($prodcatid)
        )
    )
);
$postslist = get_posts( $args );
SO网友:offroff

如果使用get\\u posts()函数,默认值将是按日期排序。

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

foreach($latest_products as $product) {
    print "{$product->post_title}<br>\\n";
}
您还需要知道,wordpress会在您输入时自动执行类似的查询?post\\u type=url中的产品。

结束

相关推荐

Server-side sorting of posts

我在一些网站上看到,您可以通过传入以下查询在服务器端对帖子进行排序:domain.com/?sort=date 或domain.com/?sort=name我该如何处理页面上的WordPress帖子?