Ordering posts by an array

This is not possible with native API and you will probably have a rather bad time hacking up SQL for it. This would be quite trivial to re–sort posts like this after they are retrieved with PHP code. if you use get_posts() you get array you manipulate already and for a WP_Query object array of … Read more

pre_get_posts shows posts in random order sometimes

pre_get_posts is not a filter hook, it is an action. Also you should not set orderby to menu_order for posts as they are not hierarchical. Set orderby to date instead. function modify_query_amount_shown( $query ) { if ( $query->is_category ) { $query->set( ‘posts_per_page’, 30 ); $query->set( ‘orderby’, ‘date’ ); } } add_action( ‘pre_get_posts’, ‘modify_query_amount_shown’ ); Now … Read more

Using abs() with custom field in orderby statement

Custom Class to do the custom parse class AS_Query extends \WP_Query { protected function parse_orderby($order_by) { $additional_allowed = array(); $absValue = null; if (preg_match(‘/abs\(([0-9]+)\)/i’, $order_by, $matches)) { $absValue = $matches[1]; $order_by = sprintf(‘abs((meta_value+0) – %s)’, intval($matches[1])); $additional_allowed[] = $order_by; } if (!in_array($order_by, $additional_allowed, true)) { $parent_orderby = parent::parse_orderby($order_by); if ($parent_orderby) { return $parent_orderby; } return … Read more

WP Query – order posts by meta field first and then order the rest

You should be able to name and sort your meta query clauses like this: $meta_query = array( ‘relation’ => ‘OR’, ‘foo’ => array( ‘key’ => ‘expiring_date’, ‘value’ => date(‘Ymd’), ‘compare’ => ‘>’, ‘type’ => ‘DATE’ ), ‘bar’ => array( ‘key’ => ‘expiring_date’, ‘compare’ => ‘NOT EXISTS’ ), ‘baz’ => array( ‘key’ => ‘expiring_date’, ‘value’ => … Read more

Custom post type sorting: alphabetical order

Try using $query->get() instead of get_query_var(). function my_orderby_filter($orderby, &$query){ global $wpdb; //figure out whether you want to change the order if ($query->get(“post_type”) == “portfolio”) { return “$wpdb->posts.post_title ASC”; } return $orderby; } Alternatively, you can use this one, which filters the query via the pre_get_posts hook. add_action( ‘pre_get_posts’, ‘my_orderby_filter2’ ); function my_orderby_filter2( $query ) { … Read more

How to get woocommerce selected variation from order object

Following below code according your need To get the selected variation attributes using get_variation_attributes( ) method. // Get an instance of the WC_Order object from an Order ID $order = wc_get_order( $order_id ); // Loop though order “line items” foreach( $order->get_items() as $item_id => $item ){ $product_id = $item->get_product_id(); //Get the product ID $quantity = … Read more

Check post_date in pre_get_posts

You can’t really check post_date in pre_get_posts, because, well, the action fires before (pre_…) the posts have actually been fetched (…get_posts). 🙂 But, you can use $query->set() to add date parameters to the query. This is taken from an example in the WP_Query() Codex entry: <?php function wpse54142_filter_pre_get_posts( $query ) { if ( is_feed() ) … Read more