1

I have been able to use WordPress filters for posts_join_paged, posts_where, and posts_orderby to create a query that presents a set of artists where only artists who have a last name listed in the custom-last-name post metadata appear listed by that last name. This works when used as a regular page with manual paging. However, it does not work with Jetpack's infinite scroll enabled.

Here are some of the details in the scenario. This is an archive page for the custom post type "artist". In functions.php I have the following:

add_filter('posts_join_paged','my_posts_join_paged');
add_filter('posts_where','my_posts_where');
add_filter('posts_orderby', 'my_posts_orderby');

function my_posts_join_paged($join_statement) {
    global $wp_query;
    global $wpdb;

    if ($wp_query->query_vars['post_type'] == 'artist') {
           $join_statement .= "INNER JOIN $wpdb->postmeta AS meta ON $wpdb->posts.ID = meta.post_id";
    }
    return $join_statement;
}

function my_posts_where($where_statement) {
    global $wp_query;
    global $wpdb;

    if ($wp_query->query_vars['post_type'] == 'artist') {
           $where_statement .= "AND meta.meta_key = 'custom-last-name'";
    }
    return $where_statement;
}

function my_posts_orderby($orderby_statement) {
    global $wp_query;
    global $wpdb;

    if ($wp_query->query_vars['post_type'] == 'artist') {
           $orderby_statement = "meta.meta_value ASC, $wpdb->posts.post_title ASC";
    }
    return $orderby_statement;
}

With Jetpack's infinite scroll turned on the first page is sorted properly, but then subsequent pages do not appear in last name order. It also appears that some artist posts get skipped entirely.

Any ideas how to introduce this kind of custom sort limited only to posts of a certain type in such a way that it will work with Jetpack's infinite scroll?

There is a Jetpack filter called infinite_scroll_query_args which might be helpful, but it seems that at the time it is called there is nothing that can be used to detect whether or not to make the orderby change. Note that on other infinite scroll pages on the site I want to let default ordering prevail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.