donglei1699 2015-07-21 10:06
浏览 40
已采纳

Wordpress - 分页帖子,排序方式:meta_key仅限当前类别

I trying to make a pagination links from my website.


Schema:


Serie | (category)
    |_ Temporada 1 (taxonomy: temporada)
            |_ Episodio 1 (posts 1 - meta_key: numeroepisodio)
            |_ Episodio 2 (posts 2 - meta_key: numeroepisodio)
            |_ Episodio 3 (posts 3 - meta_key: numeroepisodio)
            |_ Episodio 4 (posts 4 - meta_key: numeroepisodio)
            |_ ...
    |_ Temporada 2  (taxonomy: temporada)
            |_ Episodio 1 (posts 1 - meta_key: numeroepisodio)
            |_ Episodio 2 (posts 2 - meta_key: numeroepisodio)
            |_ Episodio 3 (posts 3 - meta_key: numeroepisodio)
            |_ Episodio 4 (posts 4 - meta_key: numeroepisodio)
            |_ ...

Another Serie 1
    |_ ...
Another Serie 2
    |_ ...

Problem:


When I create my posts, the pagination are order by DATE. Can help me to make this order by meta_key, in same taxonomy only for current category?


Code:


<?php
    $prev_post = get_adjacent_post( true, '', true );
    $next_post = get_adjacent_post( true, '', false );

    if ( !is_a( $next_post , 'WP_Post' ) ) {
        $query_args = array (
        'post_type'         => 'post',
        'posts_per_page'    => -1,
        'meta_key'          => 'numeroepisodio',
        'orderby'           => 'meta_value meta_value_num',
        'order'             => 'DESC',
        'paged'             => $paged,
        'tax_query'         => array( 
                                array( 'taxonomy' => 'categry'),
                                array( 'taxonomy' => 'temporada') 
                        )
    );
        $oldest = get_posts($query_args);
        $next_post = $oldest[0];
    }
    if ( !is_a( $prev_post , 'WP_Post' ) ) {
        $latest = get_posts($query_args);
        $prev_post = $latest[0];
    }
?>
<?php if ( is_a( $prev_post , 'WP_Post' ) ) : ?>
<a href="<?php echo get_permalink( $prev_post->ID ); ?>"><div id="temporadas-dropdown"><i class="fa fa-caret-left"></i> ANTERIOR</div></a>
<?php endif; ?> 

<?php if ( is_a( $next_post , 'WP_Post' ) ) : ?>
<a href="<?php echo get_permalink( $next_post->ID ); ?>"><div id="temporadas-dropdown">SIGUIENTE <i class="fa fa-caret-right"></i></div></a>
<?php endif; ?> 

Here is a live example: http://series.enlatino.net/ver-online/silicon-valley/

Thanks so much in advice!

  • 写回答

1条回答 默认 最新

  • dontoften8899 2015-07-22 01:20
    关注

    I found this:

    <?php
    /** Plugin Name: (#73190) Alter adjacent post link sort order */
    function wpse73190_adjacent_post_sort( $orderby )
    {
        return "ORDER BY p.menu_order DESC LIMIT 1";
    }
    add_filter( 'get_previous_post_sort', 'wpse73190_adjacent_post_sort' );
    add_filter( 'get_next_post_sort', 'wpse73190_adjacent_post_sort' );
    

    from wordpress.stackexchange.com - which will explain it better, so check it out... the accepted answer, but essentially you might be able to alter the query with that filter, and there's different filters - where, join, sort for both previous and next.


    Totally different answer:

    I am not really a fan of doing it manually but this might work - I did not test it though and my full understanding of taxonomies and the wordpress DB is somewhat lacking.

    function get_adjacent_episode($post, $prev_or_next) {
        global $wpdb;
    
        $prev_or_next = ($prev_or_next === 'previous') ? 'DESC' : 'ASC';
        $operator = ($prev_or_next === 'previous') ? '<' : '>';
    
        $cur_episode = get_post_meta($post->ID, 'numeroepisodio', true);
    
        $query = sprintf("select p.ID from $wpdb->posts p 
            inner join $wpdb->postmeta m 
            on p.ID = m.post_id
            inner join $wpdb->terms_relationships r
            on p.ID = r.object_id
            where r.term_taxonomy_id = (select term_taxonomy_id from $wpdb->term_taxonomy where taxonomy = '%s' limit 1) 
            and p.post_type = 'post' and m.meta_key = '%s' and m.meta_value %s %s
            order by m.meta_key %s limit 1", 'temporada', 'numeroepisodio', $operator, $cur_episode, $prev_or_next);
    
        $adjacent_post = $wpdb->get_results($query);
        return $adjacent_post;
    }
    

    Call it:

    $next_post = get_adjacent_episode($post, 'next');

    $prev_post = get_adjacent_episode($post, 'previous');

    Note that the query has only select p.ID, so you'd only be able to access $next_post->ID but you could expand this select to capture more of the result if needed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢