• Hello,

    i have some posts where the title is written like this : firstname lastname

    so i would like to order my posts by the “lastname” part of the title each posts.

    i’m not a developer.

    thank you for your help.

    • This topic was modified 2 years, 5 months ago by raidje.
    • This topic was modified 2 years, 5 months ago by raidje.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello Raidje

    Generally, it’s required the woocommerce installed for your site to order any posts.

    Instead of posts, you can manage it by the WooCommerce products to order anything.

    Thanks.

    Thread Starter raidje

    (@raidje)

    thank you but it’s not a solution for me. i don’t sale anything and i don’t want to use woocommerce just for this.

    Moderator bcworkz

    (@bcworkz)

    Does the last name exist on its own in any post (or order) data, such as post meta or custom fields (or related data such as a registered user)? Extracting the last name out of a title for the purpose of ordering posts gets a bit involved. Ordering by a post meta field involves a relatively simple change to the posts query. Extracting last name out of a title involves development a custom ordering algorithm. Not as complicated as it may sound, but more involved none the less.

    Such an algorithm is also much less efficient in delivering ordered results. If a lot of posts are involved, it might be worth creating a custom field for the purpose if one does not already exist. A one time script could do so for existing posts. Some custom code would be needed to do so for new posts. Likely beyond reach for non-developer DIY, but IMO would be the optimal solution if you have some spare funds for hiring some professional help.

    For the record, soliciting paid help in these forums is not allowed. I’m only pointing out that it’s an option available to you through other channels. Things would be much easier to sort by first name, actually sort by entire post title. Same first names would then be sorted secondarily by last.

    Thread Starter raidje

    (@raidje)

    bcworkz, thank you for your reply.

    so you suggest i could use the custom field to register the last name. This tag could be unique in fact.

    so with that how to re-order posts from a specific category by the custom field ?

    Moderator bcworkz

    (@bcworkz)

    Ordering by a custom field value still requires some custom coding. But the basic code is relatively simple, developer-type skills not required. The complication is in having the code only order the query you want so ordered and not everything else. Here’s an example that orders only back end queries for post type “order” by a field keyed as “lastname”:

    add_action('pre_get_posts', 'rdj_order_orders', 999 );
    function rdj_order_orders( $query ) {
       if ( is_admin() && $query->is_main_query() && 'order' == $query->get('post_type')) {
          $query->set('meta_key', 'lastname');
          $query->set('orderby', 'meta_value');
       }
    }

    This can go in functions.php of your theme (preferably that of a child theme). Alter as needed to suit your specific need.

    Thread Starter raidje

    (@raidje)

    thank you for your help bcworkz

    it works, great support from you !!

    have a nice day

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘custom order with a part of the post title’ is closed to new replies.