• Resolved ViscoDesign

    (@viscodesign)


    Hey folks!

    I want to display all my custom posts within a taxonomy and also list them alphabetically. I don’t want to change the setting: Reading > Blog posts show at most.

    After searching, I’ve found this custom function code (added to functions.php) to make it work …

    function customize_directory_listings ( $query ) {
        if (($query->is_main_query()) && (is_tax('resource_category')))
    
        $query->set( 'posts_per_page', -1 );
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
    
    add_action( 'pre_get_posts', 'customize_directory_listings', 1 );

    However, this also has the effect of displaying every post on every page in alphabetical order – even the posts in the Admin area. I can’t figure out why it’s doing that when the function specifically calls for is_tax

    Does anyone know;
    a) why that’s happening, and
    b) how to fix it so it only shows on the custom taxonomy page (taxonomy-resource_category.php)

    Here’s my site in case it helps understand what I’m trying to do. It’s the Resources section: https://whistlerweddingmagazine.com/resource-category/venues/

    As you can see it works, but it also works for the main posts too which is not what I want.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter ViscoDesign

    (@viscodesign)

    Just wondering if anyone has any quick advice on where to start problem solving this one? Thanks!

    Thread Starter ViscoDesign

    (@viscodesign)

    Ok, so I figured it out. Just in case anyone needs the code, simply add this to your functions.php, changing ‘custom_taxonomy_name’ as necessary …

    function custom_post_type_listings ( $query ) {
    	if ( is_admin() || ! $query->is_main_query() )
        	return;
    
    	if ( is_tax('custom_taxonomy_name') ) {
    		$query->set( 'posts_per_page', -1 );
    		$query->set( 'orderby', 'title' );
    		$query->set( 'order', 'ASC' );
    		return;
    	}
    }
    add_action( 'pre_get_posts', 'custom_post_type_listings', 1 );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display all custom taxonomy posts and sort alphabetically’ is closed to new replies.