Displaying posts sorting by a custom criterion

Save the order in a Custom Field, then modify the main query to order on that custom field via pre_get_posts. Example for posts page: function wpa_custom_order( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( ‘meta_key’, ‘your_order_key’ ); $query->set( ‘orderby’, ‘meta_value’ ); } } add_action( ‘pre_get_posts’, ‘wpa_custom_order’ );

Enable the admin to sort custom taxonomy

Thanks to this great tutorial, I just found the answer myself. WordPress offers the action hooks sb_reference_category_add_form_fields, sb_reference_category_edit_form_fields, create_sb_reference_category and edited_sb_reference_category (these are named after the custom taxonomy, in my case sb_reference_category). With these hooks, it is easy as pie to add and save a custom field to the taxonomy.

WP_Query order result by date AND meta_value

While WordPress does currently support multiple orderby it doesn’t actually support multiple order respectively. This was fixed very recently, see ticket #17065 (Independent ASC/DESC in multiple ORDER BY statement.) and will be available in 4.0 release with new array syntax: ‘orderby’ => array( ‘title’ => ‘DESC’, ‘menu_order’ => ‘ASC’ ).

Sort posts by multiple meta key values

I think the best way would be to store the complete date-time in one meta field, saving it as an timestamp in the metafield and converting it by php datetime object functions for displaying. Step 1) Create a Metabox with a text input for entering the date + time. You can find a good introduction … Read more

How to set order for a custom post type

As @Pat said, pre_get_posts is action hook, it wont work that way. But you don’t need to use pre_get_posts hook in functions.php. Remove this functions and filter from functions file and use these arguments in you WP_Query. orderby instead of ID, use menu_order. $args = array( ‘post_type’ => ‘bxslider’, ‘orderby’ => ‘menu_order’, ‘order’ => ‘ASC’, … Read more

Order posts by condition

As Tom said, you should never use query_posts and should almost always pass any arguments as an array. I think this will achieve what you want: <?php // Arguments $args = array(‘ ‘nopaging’ => true, ‘terms’ => ‘featured’, ‘posts_per_page’ => 15, ‘orderby’ => ‘rand’, ‘order’ => ‘DESC’, ‘); // The query $the_query = new WP_Query( … Read more

Custom post type archive with rand and meta_value

You could use the ‘posts_orderby’ filter: function custom_loops($query) { if ( is_post_type_archive(‘my_post_type’) ){ $query->set( ‘posts_per_page’, 40); $query->set( ‘meta_key’, ‘custom_meta_sponsored’); $query->set( ‘orderby’, ‘custom_meta_sponsored’); $query->set( ‘order’, ‘DESC’); add_filter( ‘posts_orderby’, function ( $orderby, $query ) { if ( $query->get( ‘orderby’ ) != ‘custom_meta_sponsored’ ) return $orderby; global $wpdb; $orderby = $wpdb->prepare( ‘CASE WHEN ‘ . $wpdb->postmeta . ‘.meta_value … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)