2

I need to order my Wordpress posts (that happen to be Woocommerce products) alphabetically, but I want to ignore certain words, eg. 'The' and 'A' and 'An'.

Is there a function or filter that can be targeted to the custom post query on top of orderby=ASC to ignore certain chosen words?

<?php
// My custom query
$args = array( 'post_type' => 'product', ... );
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post(); ?>
3

Ok, worked it out via a little workaround.

  1. Add a new sorting function in Woocommerce, something like: https://gist.githubusercontent.com/bekarice/0df2b2d54d6ac8076f84/raw/wc-sort-by-postmeta.php works nicely, but remove the bit about meta values and keys since we're not sorting by custom fields (though that could be a good workaround too).
  2. Change the orderby to be 'name'. Eg post slug rather than post title.
  3. Change all the products to remove the offending words from the slugs/permalinks so that /a-childs-story becomes /childs-story
  4. Choose the new sorting option as the default option in Woocommerce settings (products).

Here's the final code for your functions.php:

 /**
  * Cool orderby function from Monkey Puzzle. Adapted from:
  * Tutorial: http://www.skyverge.com/blog/sort-woocommerce-products-custom-fields/
 **/
function monkey_ordering_args( $sort_args ) {

$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
switch( $orderby_value ) {

    // Name your sortby key whatever you'd like; must correspond to the $sortby in the next function
    case 'slug':
        $sort_args['orderby']  = 'name';
        // Sort by ASC because we're using alphabetic sorting
        $sort_args['order']    = 'asc';
        break;          
}

return $sort_args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'monkey_ordering_args' );


// Add these new sorting arguments to the sortby options on the frontend
function monkey_add_new_orderby( $sortby ) {

// Adjust the text as desired
$sortby['slug'] = __( 'Sort by name', 'woocommerce' );

return $sortby;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'monkey_add_new_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'monkey_add_new_orderby' );
1

Sometimes the easiest solution to this kind of problem is to use a meta data field in which you store the relevant value by which you want to sort. For "the stackexchange" you store "stackexhange" but for "mark kaplun" you just store "mark kaplun". This should be very trivial code to write. Once you have the sort values in place all that is left to do is sort by the meta field.

The biggest advantage of this approach is performance since you can in this way leave all the heavy sorting to the MYSQL and in addition it is much easier to implement paging or additional filter.

  • Thanks Mark, that's where I was headed and then realised the slug/permalink would be an even better fit, since it's automatically created and only requires editing of offending products (ones with 'the' and 'a' etc). – Monkey Puzzle Jan 15 '15 at 5:06
0

WordPress runs with MySQL and it's not really possible to do using MySQL: https://stackoverflow.com/questions/3252577/how-to-sort-in-sql-ignoring-articles-the-a-an-etc

https://stackoverflow.com/questions/1285849/custom-order-by-to-ignore-the

Every good answer suggest a new column which not ideal to the WP.

You would be able to create a function to execute by PHP, and run it after the MySQL. So it can fix the problem, or maybe using JSON to load your products/posts and a Java Script script, like they Table Sorter here: http://mottie.github.io/tablesorter/docs/example-parsers-ignore-articles.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.