Tag Info

Hot answers tagged

20

You can use a filter to modify the ORDER BY statement of WP_query. That way you can manually set the query to use ORDER BY RAND($seed); Mysql RAND() accepts a seed as an optional argument. Using a seed, it will return the same randomized result set each time. So you can generate a random number on the first page load then store it in a SESSION variable ...


15

orderby should be meta_value_num, or meta_value, not the name of the key. See WP_Query orderby parameters.


13

OK - in the end turned out to be fairly simple - as I'd had some kind of mental block - menu_order is a variable in the $post object (thanks to @brady for reminding me of that). @scribu's post on creating sortable column values then gives the rest. So, assuming the custom post type is called header_text, these are the functions and hooks that are needed: ...


9

'meta_value_num' is not magic, it cast a value as numeric, however, to be properly casted as numeric, a value must be numeric compatible. In OP is said that the field values are "numeric" but I bet there's something not numeric e.g. a currency symbol like "10 $" or "$ 10.00", or other symbol like "10-". Also consider that using a thousand separator most ...


8

The OrderBy argument can take more then one parameter so the solution was to change : 'orderby' => 'meta_value_num', to: 'orderby' => 'meta_value meta_value_num',


8

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


7

Post list in admin (edit.php) use a normal WP_Query, just like frontend can be changed using pre_get_posts. add_action('pre_get_posts', 'reorder_my_cpt'); function reorder_my_cpt( $q ) { $s = get_current_screen(); // change 'book' with your real CPT name if ( is_admin() && $s->base === 'edit' && $s->post_type === 'book' ...


7

Almost the same approach One Trick Pony has chosen, but I am using translated names and uasort() (to preserve the keys): add_filter( 'editable_roles', 't5_sort_editable_roles' ); /** * Array of roles. * * @wp-hook editable_roles * @param array $roles * @return array */ function t5_sort_editable_roles( $roles ) { uasort( $roles, ...


6

When declaring your custom post type using the register_post_type function, you have to add 'page-attributes' to the support field, like in the following example: register_post_type('myposttype', array('supports' => array('title', 'editor', 'page-attributes'))); You'll need to add any other supported meta boxes as well to the 'supports' field, see ...


6

I downloaded this plugin and took a look and it does work as advertised for me. The function it uses for displaying the page list is: wp_list_pages(array( 'sort_column'=>'menu_order', 'depth'=>'4', 'title_li'=>'', 'exclude'=>$exclude )); ?> Which is very straightforward and does very explicitly order ...


5

I'm not sure about a plugin, but you can definitely add custom meta that allows you to set the event date, or even an event daterange (my preferred way of handling this is an event date and a duration, cuts down on calculations in the backend and is generally easier to understand, but ymmv). This allows you to keep your post dates in order while maintaining ...


5

Edit: Added example of outputting the attachment's id. Assigned it to the variable $attachment_id, since that's how the core code refers to it. Note that the $post object (for the attachment) is passed into the callback of the attachment_fields_to_edit filter, so you'll have access to all of the attachment's properties. Yes, this can be done without ...


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

The nav_menu_items are already sorted by menu_order. When you go into the Appearance -> Menus and arrange the order menu the menu_order of each menu item gets updated. To change the orderby parameter in the query that displays the menu you can use pre_get_posts to alter it. Example: add_action( 'pre_get_posts', 'wpse_sort_nav' ); function wpse_sort_nav( ...


5

The WP Postviews plugin is one of the most used to record post views. Then you can sort by amount by; <?php if (function_exists('get_most_viewed')): ?> <ul> <?php get_most_viewed(); ?> </ul> <?php endif; ?> Or pass in the variables to the URL: http://example.com/?v_sortby=views&amp;v_orderby=desc Or even run a ...


4

Zlatev meta_key is the name of the custom post type, and is a VARCHAR(255) meta_value is the value of the key, and is a longtext With your query you are not ordering by values, but by the name of the field. The query works for _thumbnail_id because you are using LEFT JOIN and the CPT could be null. You could see if you run this query SELECT ...


4

Quite a specific feature, never seen any plugin. But it's a matter of taking a drag'n'drop plugin and adapting it. Here, a stripped and adapted version of My Link Order. Description and to-do: A new submenu is created in Users, My Users Order. In the plugin page, execute the function orderUsers() in your browser console, you'll see the current order as ...


4

You can add code below to the beginning of loop.php add_action('pre_get_posts', 'wpse_change_post_order'); function wpse_change_post_order($query){ $query->set('order','ASC'); $query->set('orderby','date'); } the oldest posts will be in the home page.


4

Why don't you use WordPress get_posts? You can add the parameter orderby => 'title' as you want. $args = array( 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'title' ); $posts = get_posts( $args ); print_r($posts); If you want the sql you can just add ORDER BY post_title: SELECT * FROM $wpdb->posts ...


3

$args = array( 'post_type' => 'testimonials', 'posts_per_page' => 4, 'orderby' => 'post__in', 'post__in' => array(883, 563, 568, 106); ); Using post__in within the orderby value it will honour the order of the array of IDs passed in post__in


3

First, I would solve your problem using WPQuery. Best to go there as opposed to query_posts Second, once you acknowledge and accept that WP Query is your friend for as long as you are within the wonderful world of WP theming, then you have to do as follows to solve your particular pickle: if(is_category('your_category') : $args = array( 'post_type' ...


3

SOLUTION Thanks to /u/G.M. /** Add New Field To Category **/ function extra_category_fields( $tag ) { $t_id = $tag->term_id; $cat_meta = get_option( "category_$t_id" ); ?> <tr class="form-field"> <th scope="row" valign="top"><label for="meta-order"><?php _e('Category Order'); ?></label></th> ...


3

First, you need to store the actual login date, because this is not stored by default. You can use this code to do that(use it in your functions.php) add_action('wp_login','user_last_login', 0, 2); function user_last_login($login, $user) { $user = get_userdatabylogin($login); $now = time(); update_usermeta( $user->ID, 'user_last_login', ...


3

Apparently there's a filter that you can use in the get_editable_roles function (which is called in that page): add_filter('editable_roles', function($roles){ // sort alphabetically (ignores case) usort($roles, function($a, $b){ return strcasecmp($a["name"], $b["name"]); }); return $roles; });


3

Starting in WordPress 3.5, the orderby parameter will allow the value post__in to sort by the order of the post__in parameter, just like in your example. It may not be ideal to wait or require 3.5+, but this will almost certainly be the best and easiest way to do what you're looking to do. Here's the relevant trac ticket if you want the details.


3

You can do some tricky thing with PHP. Here is the algorithm you could use. Query posts to get posts as alphabetical order. for/while loop starts. $t = Get first character of the title string. $temp = '' empty string. if $t != $temp echo $t. set $t = $temp. endfor/endwhile. Hope it make sense. The main idea is you check the first character of the title ...


3

The WP_Query page on the Codex has a section on Order & Orderby Parameters. If you want to order the query by a custom field you should add 'orderby' => 'meta_value' AND you must also specify the custom field (called your_date_field in the above example) using the meta_key key in the query. $timeline_query = new WP_Query(array( 'post_type' ...


3

Try: $postslist = get_posts( 'orderby=ID&numberposts=15' ); Here, we're ordering by the ID, which should relate to when the posts were added to the database.


3

You second query performs an entirely new query and does not have the terms set. Besides, it's not as efficient to 'redo' the query. Instead, hook into pre_get_posts and change the order there: function change_order_for_events( $query ) { //Check if currenty query is the 'main query' and event type taxonomy being viewed if ( ...


3

I've used the below plugin and it has worked really well for me: Post Types Order It just adds a "Reorder" option in the sub-menu for posts and custom post types and has drag/drop functionality.



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