WordPress – Pagination post, order by: meta_key only from current category

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?

Read More

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!

Related posts

1 comment

  1. 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.

Comments are closed.