RELATED TO: RealHomes Theme

Sorting properties in the WordPress admin panel is essential for managing your RealHomes theme efficiently. By default, WordPress sorts posts by date in descending order, but you might want to change this to suit your needs. Here’s a guide on modifying the default sorting behaviour for properties in the RealHomes theme using custom code.

Default Sorting Code Example

The following code snippet sets the default sort order for the ‘property’ post type to orderby=date and order=desc(newest properties first):

if ( ! function_exists( 'inspiry_set_default_property_sort_order' ) ) {
	function realhomes_set_default_property_sort_order() {
		// Only modify the URL for the 'property' post type admin page
		if ( isset( $_GET['post_type'] ) && 'property' === $_GET['post_type'] ) {
			// Check if 'orderby' is not already set in the URL
			if ( ! isset( $_GET['orderby'] ) ) {
				// Redirect to include default sorting parameters (orderby=date&order=desc)
				wp_redirect( add_query_arg( array( 'orderby' => 'date', 'order' => 'desc' ) ) );
				exit;
			}
		}
	}

	add_action( 'load-edit.php', 'realhomes_set_default_property_sort_order' );
}

How to Use the Code

  1. Add the Code: Paste the code into your theme’s functions.php file or a custom plugin.
  2. Customize the Parameters:
    • Ensure 'property' matches your post type used by the RealHomes theme.
    • Adjust the orderby and order parameters as needed.

Sort by Date (Ascending)

Sort properties by the oldest date first:

if ( ! function_exists( 'inspiry_set_default_property_sort_order' ) ) {
	function realhomes_set_default_property_sort_order() {
		// Only modify the URL for the 'property' post type admin page
		if ( isset( $_GET['post_type'] ) && 'property' === $_GET['post_type'] ) {
			// Check if 'orderby' is not already set in the URL
			if ( ! isset( $_GET['orderby'] ) ) {
				// Redirect to include default sorting parameters (orderby=date&order=desc)
				wp_redirect( add_query_arg( array( 'orderby' => 'date', 'order' => 'asc' ) ) );
				exit;
			}
		}
	}

	add_action( 'load-edit.php', 'realhomes_set_default_property_sort_order' );
}

Additional Notes

  • Always test your code on a staging site before applying it to a live environment.
  • Backup your site before making any changes to your functions.php file.

By implementing these customizations, you can tailor the sorting behaviour of your RealHomes theme properties to fit your specific needs. If you still encounter problems or have questions, don’t hesitate to reach out to our customer support team.