• Resolved kohncreative

    (@kohncreative)


    Hello,
    We started developing a listing directory and want to make the custom posts display by post title in alphabetical order. Currently, the custom posts are listing by post date. We have tried a few functions including:

    function alpha_order_classes( $query ) {
    if ( $query->is_post_type_archive(‘directory’) && $query->is_main_query() ) {
    $query->set( ‘orderby’, ‘title’ );
    $query->set( ‘order’, ‘ASC’ );
    }
    }

    We are also using Custom Post Type UI plugin to create each directory using the Advanced Custom Fields and even tried using the Custom Post Type UI taxonomy in another function as a category.

    At this point no custom function is working and was hoping your support could help.
    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I’m assuming you have the add_action line for this but just in case I provided it in my snippet below.

    All in all, it looks like there’s nothing wrong with your version above, but I am wondering if maybe the logic isn’t quite right. Because of that, I’ve separated out some parts below.

    add_action( 'pre_get_posts', 'alpha_order_classes' );
    function alpha_order_classes( $query ) {
    	if ( is_admin() ) {
    		return;
    	}
    	if ( ! $query->is_main_query() ) {
    		return;
    	}
    	if ( $query->is_post_type_archive('directory') ) {
    		$query->set( 'orderby', 'title' );
    		$query->set( 'order', 'ASC' );
    	}
    }
    

    I am assuming the slug for your post type is simply “directory” too.

    Thread Starter kohncreative

    (@kohncreative)

    Thanks for the quick reply. That function didn’t work either.

    The main name and slug is “directory”, we even tried the plural “directories” and the taxonomy “directory_category”.

    Still not sure what is missing.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not sure what to say at this point then, since everything I’ve ever seen with this hook, this above should be working.

    Where are you placing the code? Perhaps it’s running too late and thus not doing anything?

    Thread Starter kohncreative

    (@kohncreative)

    Hello,
    We redeveloped the page pulling the posts. The query was previously set for DESC and on the new page, we changed it to ASC and all the added posts moved in alphabetical order after saving. We added some more posts and the posts are still in alphabetical order. We greatly appreciate your help.

    Hoping we don’t have to display posts alphabetically with a second name field along with title after we import data.

    Another quick question. We have used Import any XML or CSV File to WordPress Plugin to import post data on several other website directories. Would that plugin be the best option to work with Custom Post Type UI?
    Thanks!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Well like I mentioned, the code above *should* be working, as is.

    We should be compatible with any importing plugin, since the only thing CPTUI focuses on is making sure the saved post types/taxonomies are registered when it should be.

    Importers deal with the database more than anything we do.

    Thread Starter kohncreative

    (@kohncreative)

    Sounds good, thanks!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display by post title in alphabetical order’ is closed to new replies.