Hot answers tagged

20

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 ...


11

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 ...


11

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.


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

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'); ...


10

Kaiser's answer is awesome and thorough, however just changing the ORDER BY clause isn't enough unless your menu_order matches your chronological order. I can't take credit for this, but I found the following code in this gist: <?php /** * Customize Adjacent Post Link Order */ function wpse73190_gist_adjacent_post_where($sql) { if ( !is_main_query() ...


10

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 ...


10

An easier way may be to go through and change the permalink slug on those posts that need it (under the title on the post writing screen) and then just use that for ordering instead of the title. ie. use post_name not post_title for sorting... This would also mean that your permalink may be different if you use %postname% in your permalink structure, ...


9

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


9

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


9

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 ...


9

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 ...


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 ) { unset($...


8

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 "...


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 ...


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() ...


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

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 it ...


6

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' =...


6

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 ...


6

Looking through the source code, this plugin maps the query variable orderby to the WP_Query argument of the same name, orderby. What this means is that you should be able to do the following: http://www.example.com/wordpress/?json=get_author_posts&author_slug=user&post_type=custom&include=title,custom_fields&custom_fields=date_value&...


6

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


6

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 ...


6

As stated in @ambroseya's answer, its supposed to work like that. Once you declare a meta query, even if you aren't looking for a specific value, it will only query posts with that meta key declared. If you want to include all posts sort them by the meta key, use the following code: $args = array( 'post_type' => 'news', 'orderby' => '...


6

The Problem I think there's a typo in there: The name of the filter is posts_fields not post_fields. That could explain why the title2 field is unknown, because it's definition isn't added to the generated SQL string. Alternative - Single filter We can rewrite it to use only a single filter: add_filter( 'posts_orderby', function( $orderby, \WP_Query $q ...


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

The most efficient way is probably to hook into the pre_get_posts action and add your time comparison there. This way you don't have to worry about the rest or the query options as they will still work. It should look like this (untested), and be placed in a plugin or theme functions file (not in the page template file, the query will already have been ...


5

Now that I have a better understanding of the issue, I would recommend using custom fields to sort your posts. You can have a custom field (e.g., "order") and use it to indicate the order of your posts. You then need to use a custom query to order these posts when they are displayed. You can use a custom query like the following: $args = array( '...


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



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