Sort Posts Dropdown in WordPress: Newest, Oldest, Title (Easy & Safe Code)

How to Create a Dropdown Box to Sorting Posts in WordPress

Quick answer: To add a sort posts dropdown in WordPress, place a small form with a select field above your post loop and use the pre_get_posts hook to change orderby and order based on the chosen value. The two snippets below do this safely, with no plugin, on blog, archive and search pages.

Sort posts dropdown in WordPress

How the sort posts dropdown in WordPress works

The dropdown adds a ?sort= value to the URL when a visitor picks an option. A small pre_get_posts function reads that value and changes the order of the posts WordPress loads. Because only a fixed list of values is accepted, visitors cannot inject anything into your query.

It works on the blog page, category and tag archives and search results, and it keeps the search term and category when the order changes.

Step 1: Add the dropdown form

Put this where you want the dropdown, for example above the loop in archive.php, index.php or search.php. You can also save it as sort-form.php and load it with get_template_part( 'sort-form' ).

sort-form.php
<?php
$allowed = array(
	'date-desc'          => 'Newest first',
	'date-asc'           => 'Oldest first',
	'title-asc'          => 'Title A to Z',
	'title-desc'         => 'Title Z to A',
	'comment_count-desc' => 'Most comments',
);
$current = isset( $_GET['sort'] ) ? sanitize_text_field( wp_unslash( $_GET['sort'] ) ) : 'date-desc';
?>
<form method="get" class="sort-posts">
	<?php if ( is_search() ) : ?>
		<input type="hidden" name="s" value="<?php echo esc_attr( get_search_query() ); ?>">
	<?php endif; ?>
	<?php if ( ! empty( $_GET['cat'] ) ) : ?>
		<input type="hidden" name="cat" value="<?php echo absint( $_GET['cat'] ); ?>">
	<?php endif; ?>
	<label for="sortbox">Sort by</label>
	<select id="sortbox" name="sort" onchange="this.form.submit()">
		<?php foreach ( $allowed as $value => $label ) : ?>
			<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $current, $value ); ?>>
				<?php echo esc_html( $label ); ?>
			</option>
		<?php endforeach; ?>
	</select>
	<noscript><button type="submit">Sort</button></noscript>
</form>

Step 2: Apply the order with pre_get_posts

Add this to your child theme functions.php. It uses the official pre_get_posts hook, so pagination keeps working.

functions.php
/**
 * Apply the ?sort= value from the dropdown to the main query.
 */
add_action( 'pre_get_posts', function ( $query ) {
	if ( is_admin() || ! $query->is_main_query() || empty( $_GET['sort'] ) ) {
		return;
	}
	if ( ! ( $query->is_home() || $query->is_archive() || $query->is_search() ) ) {
		return;
	}
	$allowed = array( 'date-desc', 'date-asc', 'title-asc', 'title-desc', 'comment_count-desc' );
	$sort    = sanitize_text_field( wp_unslash( $_GET['sort'] ) );
	if ( ! in_array( $sort, $allowed, true ) ) {
		return;
	}
	list( $orderby, $order ) = explode( '-', $sort );
	$query->set( 'orderby', $orderby );
	$query->set( 'order', strtoupper( $order ) );
} );

Why this version is safer than the usual snippet

  • Many older snippets print $_GET values straight into the page, which allows cross-site scripting. Here every value is escaped or converted to a number.
  • Older code used order=dsc, which WordPress ignores. The correct value is DESC.
  • The order is applied on the server with pre_get_posts, so it also works with pagination and without JavaScript.

Add more sort options

To sort by an ACF or custom field, add an option such as price-asc to both lists, then in pre_get_posts set meta_key to your field and orderby to meta_value_num. I use the same idea in ordering an Elementor Loop Grid by an ACF date.

Frequently asked questions

How do I add a sort posts dropdown in WordPress without a plugin?

Add a small form with a select field that submits a sort value, then use the pre_get_posts hook in functions.php to set orderby and order on the main query. Both snippets are above.

Does the dropdown work with pagination?

Yes. The sort value stays in the URL and pre_get_posts applies it to every page of results.

Can I sort posts by a custom field?

Yes. Add a new option and set meta_key and orderby (meta_value or meta_value_num) for it inside the pre_get_posts function.

Want custom filters or sorting on your site? See my WordPress services or get a free quote.

Need help with your own site? Get a free quote or see my recent builds.

Tanvir Hossain Tofan, WordPress developer

Written by Tanvir Hossain Tofan

WordPress & Shopify developer from Bangladesh with 5+ years of experience building custom ACF themes, Elementor sites, plugins and Shopify stores for clients worldwide. Author of the Toofan Stage-to-Live Sync and Talkvara AI Chatbot plugins on WordPress.org.

See my work · Hire me · WhatsApp · YouTube · LinkedIn

Previous Post

Disable Payment Gateway by Country in WooCommerce (2 Easy Snippets)

Disable payment gateway by country in WooCommerce ...

Next Post

Copy Link to Clipboard in WordPress: Easy Share Button in 3 Steps

Add a copy link to clipboard button in WordPress w ...

If you find it useful

Buy me a coffee

ACF

Blog

Elementor

HTML JQuery

PHP

Tips & Tricks

WordPress

AI Chatbot for WordPress: Free vs Paid, and Which One You Need

Free vs paid AI chatbot for WordPress: how each op ...

Block Email Domains in Fluent Forms: 2 Easy PHP Snippets

Block email domains in Fluent Forms on all forms o ...

WordPress Speed Optimization: How I Took a Site From 30 to 99 on PageSpeed

How I took a WordPress site from 30 to 99 on mobil ...

WordPress Maintenance Checklist: Weekly, Monthly and Yearly Tasks

The WordPress maintenance routine I use for client ...

AI Agent for WordPress: What It Can Do and How to Add One to Your Site

What an AI agent for WordPress can really do, the ...

Back to top