1

I hope someone can help me with this. I have been reading other posts, the Codex, and trying others code, but can't fix my query.

I am creating a Page that contains a list of authors. The authors are in two categories, and I need to sort by last name, first name. I want to sort them by the custom field wpcf-sortname (from the Types plugin).

I get the correct results from the query, but the results are sorted by ID.

Note: I'm not very good with queries, but any help will be appreciated.

I've tried:

<?php query_posts(array('category__and'=>array(48,49),'meta_key'=>wpcf-sortname,'orderby'=>meta_value,'order'=>ASC,));

if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink() ?>" class="participants"><?php the_title(); ?></a><br />
<?php endwhile; // end of the loop. ?>

And:

<?php
$args = array( 'category__and'=>array(48,49),'meta_key'=>wpcf-sortname,'orderby'=>wpcf-sortname,'order'=>ASC,'posts_per_page'=>-1);
$postslist = get_posts( $args );
foreach ($postslist as $post) :  setup_postdata($post); ?> 
<div>
        <a href="<?php the_permalink() ?>" class="participants" style="color:red"><?php the_title(); ?></a><br /> 
</div>

1 Answer 1

2

Try with this

<?php query_posts(array('category__and'=>array(48,49),'meta_key'=>'wpcf-sortname','orderby'=>'meta_value','order'=>ASC,));if ( have_posts() ) while ( have_posts() ) : the_post(); ?><a href="<?php the_permalink() ?>" class="participants"><?php the_title(); ?></a><br /><?php endwhile; // end of the loop. ?>

I have just put the single quote in metakey and orderby field. I think this will work for you.

Sign up to request clarification or add additional context in comments.

2 Comments

All my data is being sorted in the right order now! However, I am noticing a weird issue that for some reason one of the author's names doesn't appear. It only occurs in one category. Any idea why? <?php query_posts(array('category__and'=>array(48,37),'meta_key'=>'wpcf-sortname','orderby'=>'meta_value','order'=>ASC,));if ( have_posts() ) while ( have_posts() ) : the_post(); ?><a href="<?php the_permalink() ?>" class="participants"><?php the_title(); ?></a><br /><?php endwhile; // end of the loop. ?>
Just figured out why, looks like if I don't have the custom field filled out, the post doesn't show.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.