Tag Info

Hot answers tagged

13

Understanding the internals The "sort" order of adjacent (next/prev) posts is not really a sort "order". It's a separate query on each request/page, but it sorts the query by the post_date - or the post parent if you have a hierarchical post as currently displayed object. When you take a look at the internals of next_post_link(), then you see that it's ...


10

You can try this: add_filter('posts_orderby', 'enforce_specific_order'); $posts = get_posts($args); remove_filter( current_filter(), __FUNCTION__ ); function enforce_specific_order($orderby) { global $wpdb; return "FIND_IN_SET(".$wpdb->posts.".ID, '1,3,8,4,12') ASC"; }


10

This seems to be a bug in Wordpress. Wordpress actually modifies the meta_query if you specify orderby and meta_key as query vars. Normally this modification adds the new meta_key as the first array in meta_query array and hence the orderby is applied to the first meta key specified in meta_query. But when you modify orderby, meta_key and meta_value ...


9

See the follow-up post: http://scribu.net/wordpress/sortable-taxonomy-columns.html


9

If you change your if statement to use $query->is_main_query() like the example on the is_main_query() codex page, does that resolve the issue? The nav menus are created by a query, so usually this problem stems from the pre_get_posts interfering with the menus query. Hence, I wonder whether that check isn't working.


9

Actually, with a little digging this is quite easy to do. WooCommerce has already set up a filter so all you have to do it add a small snippet in your functions.php file. // Modify the default WooCommerce orderby dropdown // // Options: menu_order, popularity, rating, date, price, price-desc function my_woocommerce_catalog_orderby( $orderby ) { ...


8

When you create a gallery via the 3.5 media manager, menu_order is no longer used to save the order of the images, the order only exists in the shortcode you insert when you click the Insert Gallery button, via the ids= attribute. This is to accommodate the fact that you can now add multiple galleries to a single post- ordering via menu_order wouldn't work ...


8

once up a time i did a client project where i had to have archives by first letter. thinking back i'm wondering if shouldn't have just created a hidden taxonomy and then saved the first letter as a term in that taxonomy. anyway, here's what i actually did: /* * Function Create Array of Letters that have post titles (for archive) */ /* When the post ...


7

The easiest way to do this is to use a hook (the pre_get_posts hook) to change the order. But you should check that the query is one for which you do want to alter the order! (is_archive() or is_post_type_archive() should be sufficient.) For instance, put the following in your theme's functions.php... add_action( 'pre_get_posts', 'my_change_sort_order'); ...


7

Nav menus are also generated by a WP_Query, so in your pre_get_posts callback function you need to check if the $query you're altering is the main query. The easiest way to do this would probably be to do this right at the beginning of the function: if ( ! $query->is_main_query() ) return $query; Also note that in your jh_popularity_sort_query() ...


7

I needed this requirement a while back. There's a selection of plugins available, but nothing took my fancy. They were (in my opinion) either too bloated, poorly written, or the UI was either lacking or overly complex. Then I stumbled upon Term Menu Order. Essentially it adds a menu_order field to the terms database, and you use it the same way as the menu ...


6

Okay, I was determined to find a way to do this, and I think I've got it. I had hoped to find a simpler solution and avoid having to use a new WP_Query object, but it's just too ingrained into how the loop works. First, we have a couple of utility functions: // Set post menu order based on our list function set_include_order(&$query, $list) { // ...


6

Yes, use the orderby clause in your query posts. query_posts( array('orderby'=>'menu_order' ) ); See: http://codex.wordpress.org/Function_Reference/WP_Query#Order_.26_Orderby_Parameters for reference


6

EDIT: Your problem is this: When you save your meta data, you want the date to be saved as a strtotime() date, but you want it to display the date back in the old Y-m-d format. What you need to do is save it as strtotime() and then when displaying the text back in the input, you need to reverse that strtotime()so it can be displayed properly. You can do ...


5

Wordpress does this automatically if you do http://yoursite.com/?cat=1+2 (where 1 and 2 is category ids) OR http://yoursite.com/?category_name=apples+oranges (where apples and oranges is the slugs) OR something like http://yoursite.com/category/apples+oranges


5

It's because you are checking 'event_date' == $vars['event_date'] not 'event_date' == $vars['orderby]. But don't use the request filter. Instead: add_action( 'pre_get_posts', 'event_column_orderby' ); function event_column_orderby( $query ) { if( ! is_admin() ) return; $orderby = $query->get( 'orderby'); if( 'event_date' ...


5

There is an unused column, term_order, in the wp_term_relationships table that you can use to assign a custom order to the terms within your taxonomy. The order is set at 0 by default and it will take a custom query to get the order back and another solution to set the order. Example query: function wpse_order_taxes() { global $wpdb; ...


5

An 'easy' but not very good solution is to store the user's display name as well as ID, and sort by that. Obviously an update of a user's display name would prompt an update of all the domicile that user owns. Alternatively the following is an outline (untested) of what should work. The idea is to tell WordPress to sort by the meta value still (so that ...


5

See the codex for other options, but it looks like you would want to be using 'parent' as a primary sort in this case. $loop = new WP_Query( array( 'post_type' => 'myType' , 'posts_per_page' => 50 , 'orderby' => 'parent menu_order' , 'order' => 'ASC' )); This will primarily sort by parent, with a secondary sort of menu ...


5

Try this: <ul class="list-ensemble"> <?php query_posts('post_type=person&post_status=publish&meta_key=last_name&orderby=meta_value&order=ASC'); $current_letter = ''; if ( have_posts() ) while ( have_posts() ) : the_post(); $last_name = get_post_meta( $post->ID, 'last_name', true ); $letter = strtolower( substr( ...


5

You can filter SQL to change to your condition in get_adjacent_post() ( source ) : $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); Or filter link altogether in adjacent_post_link() ( source ) : echo apply_filters( "{$adjacent}_post_link", $format, $link ); PS $adjacent can be next or previous.


5

Thank you everyone for your help! In the end the query below got me the results I desired - which was to show and sort the posts by a custom field of "publication_date" first - sorting by the date, and if there were multiple of the same date (say, 4 marked June 2013), it would sort those by title. Then, after it runs through all the posts that have the ...


5

Multiple orderby with different sort order values are possible as of WordPress 4.0, take a look at the codex page for WP_Query - Order - Orderby Parameters. There is an related Make WordPress Core article too. The following example would comply to what you want: $query = new WP_Query( array( 'orderby' => array( 'name' => ...


5

Found the answer from the WordPress docs. For what the query is ordered by, I use: global $wp_query; echo $wp_query->get('orderby'); For the order by which the posts are queried: global $wp_query; echo $wp_query->get('order');


4

It's common to utilize the WordPress navigational menu UI to get the drag/drop feature for free. I think there are already some answers, on this site, explaining this better than I can, but I can't seem to find them for you. So let my try to construct one simple example: We go to Appearance/Menus and create a menu with e.g. the mylist menu id. Then we ...


4

Adding Custom Sortable WordPress Admin Table Columns by Taxonomy Terms To achieve adding a custom sortable column to the WP_List_Table of your post type within the WordPress administration back-end dashboard, you will need to do the following... Replace all occurrences of "YOUR-POST-TYPE-NAME" with your actual post type name. Replace all occurrences of ...


4

Looking through the source code, this plugin maps the query variable orderby to the WP_Query argument order_by. What this means is that you should be able to do the following: ...


4

It can be done sorting the global $submenu. The sorting that's applied resets the key number of the sub-array $submenu['options-general.php'], which is: array 'options-general.php' => array 10 => array 0 => string 'General' 1 => string 'manage_options' 2 => string 'options-general.php' ...


4

If you want the meta box to appear below the content area, use the context parameter 'normal'. See Function Reference/add meta box - Parameters. Initially, your meta box should only go to the side bar if the context parameter is set to 'side'. As far as making it impossible for the user to drag it to the side, study the answer at "Block metabox - No ...


4

Why don't use built-in functionality of PHP? Put the following line right before the foreach: usort($members, create_function('$a, $b', 'return strnatcasecmp($a->last_name, $b->last_name);')); References: usort create_function strnatcasecmp



Only top voted, non community-wiki answers of a minimum length are eligible