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


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


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

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

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


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


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


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

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


5

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


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

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

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

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.


4

First in your category.php (depending on your theme files) add a simple form to let the user sort A-Z or Z-A: <form action="" name="custom_order" method="POST"> <p> sort order: <select name="CU_Order" id="CU_Order"> <option value="ASC">A-Z</option> <option ...


4

Just incase if you have not checked out the plugin developers page but heres something that you could try if (function_exists(ckrating_get_comments)) { $post_id = $post->ID; $mycomments = ckrating_get_comments( "post_id=$post_id&status=approve& orderby=comment_karma&order=DESC"); } else $mycomments = null; wp_list_comments(array(), ...


4

This is indeed an error, and probably caused by the child_of argument. Because child_of requests the whole subtree of the given page (not only the direct children, but also children of those children and so on), WordPress first queries for all the pages and then selects a subset of those pages. The first query respects the order, but the second subselection ...


4

I was looking for the same thing and figured I would copy code provide by Mike below which he claims is still very much in alpha since its only been him that has been using it. I have tested it and it seems to work well for me. I'm sure there are use-cases they do not yet handle. Maybe Mike will comment in here and provide additional help on how you can ...


4

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



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