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

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


8

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.


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


7

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


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


5

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


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

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


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

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

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


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

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


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

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

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


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

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

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

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.



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