0

我创建了一个帖子类型“事件”,它使用自定义字段“基准”(瑞典语表示日期)。我希望此帖子类型的默认视图按此字段排序。

到目前为止,我得到了:

 register_post_type(
                'events',
                array(
                        'labels' => array(
                                'name' => __( 'Arrangemang' ),
                                'singular_name' => __( 'Arrangemang' )
                        ),
                        'supports' => array(
                                'title',
                                'editor',
                                'custom-fields'
                        ),
                        'public' => true,
                        'has_archive' => true,
                        'query_var' => array(
                                'post_type' => 'events',
                                'meta_key' => 'Datum',
                                'orderby' => 'meta_value',
                                'order' => 'ASC',
                        ),
                )
        );

但是排序不起作用。如果我在事件的存档页面上运行,print_r($wp_query->query_vars);我会得到:

Array
(
    [post_type] => events
    [error] =>
    [m] => 0
    [p] => 0
    [post_parent] =>
    [subpost] =>
    [subpost_id] =>
    [attachment] =>
    [attachment_id] => 0
    [name] =>
    [static] =>
    [pagename] =>
    [page_id] => 0
    [second] =>
    [minute] =>
    [hour] =>
    [day] => 0
    [monthnum] => 0
    [year] => 0
    [w] => 0
    [category_name] =>
    [tag] =>
    [cat] =>
    [tag_id] =>
    [author_name] =>
    [feed] =>
    [tb] =>
    [paged] => 0
    [comments_popup] =>
    [meta_key] =>
    [meta_value] =>
    [preview] =>
    [s] =>
    [sentence] =>
    [fields] =>
    [category__in] => Array
        (
        )

    [category__not_in] => Array
        (
        )

    [category__and] => Array
        (
        )

    [post__in] => Array
        (
        )

    [post__not_in] => Array
        (
        )

    [tag__in] => Array
        (
        )

    [tag__not_in] => Array
        (
        )

    [tag__and] => Array
        (
        )

    [tag_slug__in] => Array
        (
        )

    [tag_slug__and] => Array
        (
        )

    [meta_query] => Array
        (
        )

    [ignore_sticky_posts] =>
    [suppress_filters] =>
    [cache_results] => 1
    [update_post_term_cache] => 1
    [update_post_meta_cache] => 1
    [posts_per_page] => 100
    [nopaging] =>
    [comments_per_page] => 50
    [no_found_rows] =>
    [order] => DESC
    [orderby] => wp_posts.post_date DESC
)

我是否误解了 的论点query_varregister_post_type或者为什么没有按照我的要求对列表进行排序?

4

2 回答 2

0

据我了解, query_var 只能是布尔值,不处理自定义帖子类型的排序。一种解决方案是为您的帖子类型创建一个自定义存档页面并在那里修改循环,以便该自定义帖子类型的存档页面始终按元值排序。

于 2012-11-01T20:18:34.453 回答
0

我尝试修改存档页面,但最终只查询了两次帖子,所以如果查询是正确的,我连接了“parse_query”以按信息添加顺序。

稍后我可能需要更好地定义正确的,但现在它工作正常,查询只运行一次。

add_action('parse_request', 'event_sort');

function event_sort($a)
{
   if($a->query_vars AND count($a->query_vars) == 1 AND $a->query_vars['post_type'] == 'events')
   {
      $a->query_vars['meta_key'] = 'Datum';
      $a->query_vars['orderby'] = 'meta_value';
      $a->query_vars['order'] = 'ASC';
   }
}

在我发现之前parse_request我使用过parse_query

add_action('parse_query', 'event_sort');

function event_sort($a)
{
   if($a->query AND count($a->query) == 1 AND $a->query['post_type'] == 'events')
   {
      global $wp_query;
      $wp_query->set('meta_key', 'Datum');
      $wp_query->set('orderby', 'meta_value');
      $wp_query->set('order', 'ASC');
   }
}
于 2012-11-06T20:25:24.220 回答