Tag Info

Hot answers tagged

9

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"; }


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

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


6

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


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

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


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

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


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


4

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


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

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


4

WordPress does not maintain post view count by default. But you may find the following post helpful. Does wordpress have a post hit counter? You may also have a look on the WP-PostViews plugin. Many thanks.


4

As far as I can tell, there is no work-around to this at the database level. This is a problem I run into somewhat often, which is the case you need to turn an list with structure references into an an ordered array with children elements appearing immediately after their parents. This can be accomplished in PHP, but while this solution is fairly compact, it ...


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


3

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


3

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


3

Still looking myself. After some exploring I decided to use the Term Menu Order plugin by @jameslafferty. This will add menu_order to the terms table and can be used across taxonomies and templates. @TheDeadMedic seems to have enhanced this functionality over here, discussing a similar issue.


3

The display of posts is controlled by WordPress Loop. It has a lot of possible arguments, but they are rarely exposed in URL (especially if using pretty permalinks). But it is possible to make use of arguments by manually adding them to URL. For sorting see Orderby Parameters. Your example would be something like: domain.com/?orderby=date ...


3

after looking around a bit I managed it to make it work using the following: function sort_pages_by_date($a, $b){ if ($a->post_date == $b->post_date) return 0; if ($a->post_date < $b->post_date) return -1; return 1; } function get_filtered_news($pages) { usort($pages, 'sort_pages_by_date'); rsort($pages); ...


3

You need to change orderby value to meta_value in event_date_column_orderby, also I am not sure about the get('event_date') you are using, so I have replaced it in the code below to get_post_meta. I have just tested the code below and it sorts by dates just fine. // Register a new column in the admin Post area function event_date_column_register( $columns ...


3

You would need to build your own query and pass it to query_posts i.e. if your array of post ids is: array (10,21,1) // corresponding to post ids. then you can query this way: select * from wp_posts where `ID` in (10, 21, 2) order by field(ID,10,21,2) This will give you a result set in the order of your post ids array. HTH


3

I had this same problem until I realized you don't actually have to query by the event start date, only the end date. As long as the event's end date has not past it doesn't matter whether the start date is in the future or not. All you have to do is grab all the dates that have not ended yet then you can order them by their start date. This should work ...


3

Here's the final code I used with a custom taxonomy (edited based on Stephen's answer above so it actually works - I was still just querying categories with my old code): <?php $terms = get_terms('dates'); foreach($terms as $term): $args = array( 'orderby' => 'title', 'tax_query' => array( array( ...


3

Wrap the whole thing within a form and add a submit button in it: <form method="POST" action=""> <ul class="filter-sub-dropdown"> ... </form> Use term IDs as checkbox values instead of slugs (you'll save a little server resources): $discipline->slug; > $discipline->term_id Then, in your functions.php file check if that ...


3

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.


3

One way would be to keep track of which years you've already printed. Using your code: <?php $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => -1, 'orderby' => 'menu_order', 'order' => ASC ); $attachments = get_posts( $args ); echo '<table ...


3

You should use pre_get_posts: add_action('pre_get_posts','wpse56753_businesses_default_order'); function wpse56753_businesses_default_order( $query ){ if( 'business' == $query->get('post_type') ){ if( $query->get('orderby') == '' ) $query->set('orderby','title'); if( $query->get('order') == '' ) ...



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