How to Dynamically Order Posts Using Query Strings and query_posts in WordPress

WPTurbo » Snippets » How to Dynamically Order Posts Using Query Strings and query_posts in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking for a way to dynamically order the posts on your WordPress site? By using the query string and the query_posts function, you can easily achieve this. Whether you want to sort your posts by date, title, or any other custom field, this method allows you to dynamically change the order with just a simple URL parameter. In this article, we will guide you through the process of using the query string and query_posts function to dynamically order your WordPress posts.

					function wpturbo_order_posts_query() {
    // Check if 'orderby' query string parameter is set
    if ( isset( $_GET['orderby'] ) ) {
        $orderby = sanitize_text_field( $_GET['orderby'] );
        
        // Define valid orderby values
        $valid_orderby = array( 'title', 'date', 'modified', 'rand' );
        
        // Check if the value of 'orderby' parameter is valid
        if ( in_array( $orderby, $valid_orderby ) ) {
            // Set the 'orderby' value in the query
            set_query_var( 'orderby', $orderby );
        }
    }
}
add_action( 'pre_get_posts', 'wpturbo_order_posts_query' );
				

The provided code snippet is used to dynamically order posts using query string parameters and the query_posts function in WordPress.

The first part of the code defines a function called wpturbo_order_posts_query(). This function is responsible for checking if the orderby query string parameter is set and valid.

Inside the function, we first use the isset function to check if the orderby query string parameter is set. If it is set, we retrieve its value using $_GET['orderby'] and assign it to the $orderby variable.

Next, we define an array called $valid_orderby which contains the valid values for the orderby parameter. This array ensures that only valid values are used for ordering the posts.

Then, we use the in_array function to check if the value of the $orderby variable exists in the $valid_orderby array. If it does, it means that the value is valid and we proceed to the next step.

Inside the in_array conditional statement, we use the set_query_var() function to set the value of the orderby parameter in the query. This ensures that the posts will be ordered according to the selected value.

Finally, we hook the wpturbo_order_posts_query() function into the pre_get_posts action. This action is triggered before the main query is executed, allowing us to modify the query parameters.

By using this code, you can create links on your website that include the orderby query string parameter, such as example.com/?orderby=title. When the user clicks on one of these links, the wpturbo_order_posts_query() function will be triggered and the posts will be ordered based on the value of the orderby parameter.

In summary, this code snippet provides a simple and effective way to dynamically order posts on your WordPress website using query string parameters and the query_posts function.

Register an account to save your snippets or go Pro to get more features.