0

I am trying to store different values into an array based on a number of conditions like so:

<?php $sort= $_GET['sort']; 

     if($sort == "title") { $args = array('orderby'=>'title','order'=>'ASC'); } 
     elseif($sort == "date") { $args = array('orderby'=>'date'); } 
     else{ $args = array('orderby'=>'date','order'=>'DESC'); } 
?>

And then I am trying to insert the variable $args into the wordpress loop using WP_Query like so:

<?php $loop = new WP_Query( array( $args, 'post_type' => 'films', 'post_parent' => 0, 'posts_per_page' => -1 ) ); ?>

    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

    wordpress loop stuff, and the end while, end if

This is not working correctly. Am I passing the array into the wordpress loop incorrectly?

3
  • You shouldn't ask new questions when there are answer to previous ones you didn't try. Commented Jun 23, 2012 at 15:44
  • Apologies...Just keep finding ways to write it better. Commented Jun 23, 2012 at 15:48
  • Correct answer is here, thanks Tomasz: stackoverflow.com/questions/11170480/… Commented Jun 23, 2012 at 15:59

1 Answer 1

0

Your passing an array, $args, into another array. WP_Query can't understand the double nested array.

Why don't you just set a variable for the value you want assigned to the orderby and order parameters based on the result of the conditionals.

if ( $sort == 'title' ) {
   $orderby = 'title';
   $order = 'ASC';
} esleif ( // ....etc

Then in your query array:

  'orderby' => $orderby, 'order' => $order, etc..
Sign up to request clarification or add additional context in comments.

Comments

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.