WordPress.org

Ready to get started?Download WordPress

Forums

Sort by meta value and then by title (3 posts)

  1. derkdec
    Member
    Posted 4 years ago #

    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!

  2. MichaelH
    Member
    Posted 4 years ago #

    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');

  3. MichaelH
    Member
    Posted 4 years ago #

    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().
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic