Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8138597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:32:31+00:00 2026-06-06T11:32:31+00:00

I am trying to insert a sorting option into my wordpress site. I already

  • 0

I am trying to insert a sorting option into my wordpress site. I already have it working, but need help using it with the wordpress loop correctly. Currently, I have:

On a page, there are options to sort alphabetically or chronologically:

<a href="?sort=date">Newest</a>
<a href="?sort=title">Alphabetical</a>

Sorting Code starts here, placed above the loop:

<?php $sort= $_GET['sort']; 
    if($sort == "title") { $order= "'orderby'=>'title','order'=>ASC'"; } 
    elseif($sort == "date") { $order= "'orderby'=>'date'"; } 
    else{ $order= "'orderby'=>'date','order'=>'DESC'"; } 
?>

note: I am pretty sure the problem lies above in the variable $order

WordPress Loop Using Variable $order as an argument

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

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

    WordPress loop stuff here

<?php endwhile; ?>

The loop displays items correctly, but the sorting links are not working. This code works very well with query_posts but I am trying to get this to work for WP_Query (above). Any ideas here?

UPDATE: This technique works great using query_posts like below (but I still need it working for WP_Query):

<?php $sort= $_GET['sort']; 
    if($sort == "title") { $order= "&orderby=title&order=ASC"; } 
    elseif($sort == "date") { $order= "&orderby=date"; } 
    else{ $order= "&orderby=date&order=DESC"; } 
?>

<?php $posts = query_posts($query_string . $order); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
       WordPress Stuff Here
<?php endwhile; ?>
<?php endif; ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:32:34+00:00Added an answer on June 6, 2026 at 11:32 am

    WP_Query expects associative array of parameters (array('param' => 'value')), whereas query_posts accepts only “query strings” ("param=value&param=value"). You are mixing both options, that’s why it doesn’t work. You need to change $order variable to be array instead of string, for example: $order = array('orderby' => 'title', 'order' => ASC');.

    Answer above is not complete and may be misleading. WP_Query docs are here: https://codex.wordpress.org/Class_Reference/WP_Query . This class accepts boths styles, but they must be somewhat different formed. I don’t know how exactly to do this, because it is not written in class docs, but you better use arrays, so:

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

    FYI to convert between both types use functions parse_str(): http://php.net/manual/en/function.parse-str.php and http_build_query(): http://www.php.net/manual/en/function.http-build-query.php .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

iam trying insert rows into a sql table in java. i need to insert
Trying to insert values with Unicode Chars into a MySQL-database using Delphi 2010 and
Im trying to insert a keyspace into cassandra using hector and the SchemaManipulation example
I've been trying insert a MapView into an ActionBar Tab , but I wasn't
I'm trying to insert objects (NSNumbers) into an NSMutable array, but when I check
I am trying to INSERT INTO a table using the input from another table.
I'm trying to insert some import lines into a python source file, but i
I have code from my ad network that I am trying insert into an
im trying to insert an array values into the file using foreach.. and if
im trying to insert data into a particular row in the database, but i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.