简体   繁体   中英

wordpress tag archive, sort order with custom post type

I added this code added to funtions.php

function tc_tag_for_cpt($query) {
  if(is_tag() && empty( $query->query_vars['suppress_filters'] ) && !is_admin()) {
    $post_type = get_query_var('post_type');
    $post_type = ($post_type) ?  $post_type : array('resources','post','page');     //,'nav_menu_item'
    $query->set('post_type',$post_type);
    return $query;
  }
}
add_filter( 'posts_orderby', 'sort_query_by_post_type', 10, 2 );
function sort_query_by_post_type( $sortby, $thequery ) {
    if(is_tag() && empty( $query->query_vars['suppress_filters'] ) && !is_admin()) {
        $thequery->set('orderby','post_type');
        if ( !empty($thequery->query_vars['post_type']) && isset($thequery->query_vars['orderby']) && $thequery->query_vars['orderby'] == 'post_type' )
            $sortby = "find_in_set(post_type, '" . implode( ',', $thequery->query_vars['post_type'] ) . "')";
    }
    return $sortby;
}

which is supposed to display results on the tag archive sorted by post type (resources is a custom post type). It does this but an error message appears on the top of the screen:

"Warning: implode() [function.implode]: Invalid arguments passed in /home/csleh3/public_html/sandbox/wp-content/themes/aodmarketing/functions.php on line 279"

which is the "implode" line.

My goal is to have the results on the tag archive sort the list by post type, not date or title.

$thequery->query_vars['post_type'] will return the specific post_type as a string like post . You can't implode that.

You should just be able to remove the part about trying to implode it:

$sortby = "find_in_set(post_type, '" . $thequery->query_vars['post_type'] . "')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM