How to Create a Dropdown Box to Sorting Posts in WordPress

With Search by Category:custom-sort-form.php Without Search by Category: custom-sort-form.php

With Search by Category:
custom-sort-form.php
With Search by Categorycustom-sort-form
<?php if ( is_search() && is_category() ) { ?>

	<select name="sort-posts" id="sortbox" onchange="document.location.href='?s=<?php the_search_query(); ?>&cat=<?php echo $_GET['cat'] ;?>&'+this.options[this.selectedIndex].value;">

<?php } elseif ( is_category() || is_archive() ) { ?>

	<select name="sort-posts" id="sortbox" onchange="document.location.href='?'+this.options[this.selectedIndex].value;">

<?php } else { ?>

	<select name="sort-posts" id="sortbox" onchange="document.location.href='?s=<?php the_search_query(); ?>&'+this.options[this.selectedIndex].value;">

<?php } ?>

		<option value="" disabled>Sort by</option>
		<option value="orderby=date&order=dsc">Newest</option>
		<option value="orderby=date&order=asc">Oldest</option>
		<option value="orderby=title&order=asc">Title Asc</option>
		<option value="orderby=title&order=dsc">Title Desc</option>
		<option value="orderby=comment_count&order=dsc">Most Comments</option>
		<option value="orderby=comment_count&order=asc">Least Comments</option>

	</select>

<script type="text/javascript">

	<?php if (( $_GET['orderby'] == 'date') && ( $_GET['order'] == 'dsc')) { ?>
		document.getElementById('sortbox').value='orderby=date&order=dsc';
	<?php } elseif (( $_GET['orderby'] == 'date') && ( $_GET['order'] == 'asc')) { ?>
		document.getElementById('sortbox').value='orderby=date&order=asc';
	<?php } elseif (( $_GET['orderby'] == 'title') && ( $_GET['order'] == 'asc')) { ?>
		document.getElementById('sortbox').value='orderby=title&order=asc';
	<?php } elseif (( $_GET['orderby'] == 'title') && ( $_GET['order'] == 'dsc')) { ?>
		document.getElementById('sortbox').value='orderby=title&order=dsc';
	<?php } elseif (( $_GET['orderby'] == 'comment_count') && ( $_GET['order'] == 'dsc')) { ?>
		document.getElementById('sortbox').value='orderby=comment_count&order=dsc';
	<?php } elseif (( $_GET['orderby'] == 'comment_count') && ( $_GET['order'] == 'asc')) { ?>
		document.getElementById('sortbox').value='orderby=comment_count&order=asc';
	<?php } else { ?>
		document.getElementById('sortbox').value='orderby=date&order=desc';
	<?php } ?>

</script>
Without Search by Category:

custom-sort-form.php
Without Search by Category:- custom-sort-form.php
<?php if ( is_search() ) { ?>

	<select name="sort-posts" id="sortbox" onchange="document.location.href='?s=<?php the_search_query(); ?>&'+this.options[this.selectedIndex].value;">

<?php } else { ?>

	<select name="sort-posts" id="sortbox" onchange="document.location.href='?'+this.options[this.selectedIndex].value;">

<?php } ?>

		<option value="" disabled>Sort by</option>
		<option value="orderby=date&order=dsc">Newest</option>
		<option value="orderby=date&order=asc">Oldest</option>
		<option value="orderby=title&order=asc">Title Asc</option>
		<option value="orderby=title&order=dsc">Title Desc</option>
		<option value="orderby=comment_count&order=dsc">Most Comments</option>
		<option value="orderby=comment_count&order=asc">Least Comments</option>

	</select>

<script type="text/javascript">

	<?php if (( $_GET['orderby'] == 'date') && ( $_GET['order'] == 'dsc')) { ?>
		document.getElementById('sortbox').value='orderby=date&order=dsc';
	<?php } elseif (( $_GET['orderby'] == 'date') && ( $_GET['order'] == 'asc')) { ?>
		document.getElementById('sortbox').value='orderby=date&order=asc';
	<?php } elseif (( $_GET['orderby'] == 'title') && ( $_GET['order'] == 'asc')) { ?>
		document.getElementById('sortbox').value='orderby=title&order=asc';
	<?php } elseif (( $_GET['orderby'] == 'title') && ( $_GET['order'] == 'dsc')) { ?>
		document.getElementById('sortbox').value='orderby=title&order=dsc';
	<?php } elseif (( $_GET['orderby'] == 'comment_count') && ( $_GET['order'] == 'dsc')) { ?>
		document.getElementById('sortbox').value='orderby=comment_count&order=dsc';
	<?php } elseif (( $_GET['orderby'] == 'comment_count') && ( $_GET['order'] == 'asc')) { ?>
		document.getElementById('sortbox').value='orderby=comment_count&order=asc';
	<?php } else { ?>
		document.getElementById('sortbox').value='orderby=date&order=desc';
	<?php } ?>

</script>
Previous Code

Disable WooCommerce Payment Gateway for Specific Country WordPress

To disable a payment gateway for a specific countr ...

Next Code

How to copy a link on a clipboard WordPress?

How to share URL and copy to clipboard with URL ch ...

Leave a Reply

Your email address will not be published. Required fields are marked *

If you find it useful

buymeacoffee

ACF

Blog

Elementor

HTML JQuery

PHP

WordPress

Create a new WordPress Administrator User via functions.php & FTP Code

Can not delete User/admin/manager Change your Need ...

Ban Email Addresses Or Domains Name in Fluent Forms Form Submission

Ban Email Domains (Ban Particular Domains) in Flue ...

Disable WooCommerce Payment Gateway for Specific Country WordPress

To disable a payment gateway for a specific countr ...

top