0

I want to order posts by title, but always show featured posts first. I have the code below to order posts by title. Now I would like to always show posts first that have the metafield 'wiloke_listgo_toggle_highlight' with the value '1'. How can I accomplish this?

/* Order Posts Alphabetically */
function prefix_modify_query_order( $query ) {
  if ( is_main_query() ) {

    $args =  array( 'title' => 'ASC' );

    $query->set( 'orderby', $args );
  }
}
add_action( 'pre_get_posts', 'prefix_modify_query_order' );
2
  • 2
    you need to set orderby to an array like that ["meta_value" => "ASC", "title" => "ASC"] and set meta_key to "wiloke_listgo_toggle_highlight".
    – mmm
    Commented Jul 9, 2018 at 16:05
  • 1
    As @mmm said but just take care because first you are reordering the admin too. and second if the meta_key not exists its won't show the posts at all. you can check if exists or if not exists but it will order the exists first all with 1 first all with 0 after and all the not exists last.
    – Shibi
    Commented Jul 9, 2018 at 16:15

1 Answer 1

0

My first attempt to help you with solving your problem would be to do something like this :

/* Order Posts Alphabetically */
function prefix_modify_query_order( $query ) {
  if ( is_main_query() ) {

    $query->set(
        'meta_query', array(
            'relation' => 'AND',
            'query_highlight' => array(
                'key'   => 'wiloke_listgo_toggle_highlight',
                'value' => '1',
                'compare' => '='
            )
        )
    );

    $query->set(
        'orderby', array( 
            'title' => 'ASC',
            'query_highlight'   => 'ASC',
        )
    );
  }
}
add_action( 'pre_get_posts', 'prefix_modify_query_order' );

Source : https://codex.wordpress.org/Class_Reference/WP_Meta_Query#Usage

The code should start ordering posts by title ASC and also take posts with your custom key with value to 1.

Note : Did not test it so I suggest you to comment this if their is any bugs.

4
  • 1
    I don’t think this is what he wanted and that it makes much sense even... I think that OP wanted to show posts with 1 first and then rest of them. Your code is showing only the posts with 1 in custom field and then you use that field for sorting (which makes no sense, since all selected posts have the same value in that field) Commented Jul 10, 2018 at 6:36
  • My understanding was that he wanted to order posts by title ASC but also by it custom meta based on a specific value. Commented Jul 10, 2018 at 10:38
  • And how is your code doing that? If every selected post have wiloke_listgo_toggle_highlight = 1, then what's the point to sort by this value? They're all equal, aren't they? ;) Commented Jul 10, 2018 at 11:11
  • Indeed. Some one Said that too. I assume the 1 Might be somehting like a true false statement. Commented Jul 10, 2018 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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