• Hi,

    I have a little problem.
    I have a query like this:
    $myposts = get_posts('numberposts=300&category=3&meta_key=datum_start&orderby=meta_value&order=ASC&meta_key=land_label&meta_value=Road2Africa');

    You can see that I want order the posts by the meta value.
    But there are a lot of posts with the same meta value.

    What I want is this:
    First order by meta value.
    If there are posts with the same meta value… then this:
    Order by title.

    How can I do that?

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • I don’t believe you can use multiple meta_key and meta_value arguments like that.

    You might have to restrict your query to just posts that have a meta_value=Road2Africa then you don’t need to orderby meta_value. Could just use this:

    $myposts = get_posts('numberposts=300&category=3&orderby=title&order=ASC&meta_key=land_label&meta_value=Road2Africa');

    Also, looking at that, the category=3 doesn’t work with the meta_key arguments. You’d need to use the category__in argument such as this example:

    <?php
    $args=array(
      'category__in' => array(3),
      'meta_value'=> 'Road2Africa',
      'meta_key' => 'land_label',
      'orderby' => 'title',
      'order' => 'ASC',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 300,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sort by meta value and then by title’ is closed to new replies.