2
votes

I try to get all the posts (with a specific custom type) ordered by the title ascending, but the SQL query generated, orders after post_date. Code:

$my_query = new WP_Query( 
   array(
      'post_type' => 'member',
      'posts_per_page' => -1,
      'orderby' => 'title',
      'order' => 'ASC',
    ) 
  );

while ($my_query->have_posts()) {
    $my_query->the_post();
    // get post excerpt
    get_template_part('content/'.get_post_type());
    // wikiwp_get_post_excerpt($post);
}

Query:

SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'member' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'closed' OR wp_posts.post_status = 'private' OR wp_posts.post_status = 'hidden') ORDER BY wp_posts.post_date ASC

I really don't understand why this is happening. I deactivated most of the plugins and tried different combinations and I had no success.

$my_query:

WP_Query Object ( [query] => Array ( [post_type] => member [posts_per_page] => -1 [orderby] => title [order] => ASC ) [query_vars] => Array ( [post_type] => member [posts_per_page] => -1 [orderby] => order [order] => ASC [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [lazy_load_term_meta] => 1 [update_post_meta_cache] => 1 [nopaging] => 1 [comments_per_page] => 50 [no_found_rows] => ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( ) [relation] => AND [table_aliases:protected] => Array ( ) [queried_terms] => Array ( ) [primary_table] => wp_posts [primary_id_column] => ID ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => [meta_table] => [meta_id_column] => [primary_table] => [primary_id_column] => [table_aliases:protected] => Array ( ) [clauses:protected] => Array ( ) [has_or_relation:protected] => ) [date_query] => [request] => SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'member' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'closed' OR wp_posts.post_status = 'private' OR wp_posts.post_status = 'hidden') ORDER BY wp_posts.post_date ASC ))

3

3 Answers

7
votes

Your code looks right. I would look to see if you have any code elsewhere, such as a filter altering the output of the query that you created. Sounds like you might have something like that because your post loop is correct.

$args = array(
    'post_type' => 'member',
    'posts_per_page' => '-1',
    'order' => 'ASC',
    'orderby' => 'title',
);

$my_query = new WP_Query( $args );

if ( $my_query->have_posts() ) {
    while ( $my_query->have_posts() ) {
        $my_query->the_post();

        get_template_part('content/'.get_post_type());
    }
}

wp_reset_postdata();
2
votes

I was facing the same issue. I solved it using 'post_title' instead of 'title'.

$my_query = new WP_Query( 
   array(
      'post_type' => 'member',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'orderby' => 'post_title',
      'order' => 'ASC'
    ) 
  );
1
votes

Please use Below code :

$my_query = new WP_Query( 
   array(
      'post_type' => 'member',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'orderby' => 'title',
      'order' => 'ASC'
    ) 
  );

You have added , at last after ASC so i think the issue was.then $my_query is where your array is so then just loop through that like below to get result as you wanted.

while($my_query->have_posts()) : $my_query->the_post();
//Your post code here to get post attributes
endwhile;wp_reset_query();