---
title: "How to Show Blog Posts Based on Custom Query in Elementor?"
url: https://theplusaddons.com/docs/show-blog-posts-based-on-custom-query-in-elementor/
date: 2023-08-28
modified: 2026-04-11
author: "Aditya Sharma"
description: "Showing blog posts based on custom query allows you to filter and showcase specific posts that are relevant to your audience's interests or needs. Custom query allows you to go..."
image: https://theplusaddons.com/wp-content/uploads/2023/08/How-to-Show-Blog-Posts-Based-on-Custom-Query-in-Elementor_-1024x536.jpg
word_count: 422
---

# How to Show Blog Posts Based on Custom Query in Elementor?

## Key Takeaways

- Dynamic Listing widget from The Plus Addons for Elementor allows users to add a custom query for displaying posts.
- Custom Query option requires advanced users to write custom PHP code for personalized post display.
- Posts can be arranged based on views using a custom query that orders posts by the 'wp_post_views_count' meta key.

Showing blog posts based on custom query allows you to filter and showcase specific posts that are relevant to your audience's interests or needs. Custom query allows you to go beyond the option provided by a widget and show posts based on your personalized query.

With the Dynamic Listing widget from The Plus Addons for Elementor, you can easily add a custom query to show posts based on your unique query.

*To check the complete feature overview documentation of The Plus Addons for Elementor Dynamic Listing widget, [click here](/docs/dynamic-listing-widget-settings-overview/).*

***Requirement  - This widget is a part of The Plus Addons for Elementor, make sure its installed & activated to enjoy all its powers.***

[LIVE WIDGET LINK](https://theplusaddons.com/elementor-listing//#dynamic-listing-wgts?utm_source=tpae&utm_medium=docs&utm_campaign=text)

> *Note: The Custom Query option is only for advanced users, comfortable with writing custom PHP code.*

For instance, we want to show posts based on views. This way most viewed posts will show first. 

To do this, follow the steps -

1. First, you have to write your custom query. 

You can do that in the functions.php file, but make sure to use a child theme and add a functions.php file there and then write your code. This will keep your code even if you update the theme.

If you are using the [Nexter theme](https://nexterwp.com/docs/nexter-theme-builder-explained/) then you can easily add custom code there, or you can use the [Code Snippets](https://wordpress.org/plugins/code-snippets/) plugin as well.

We’ll use the following code to order posts by view.

`function theplus_set_post_view($post_id) {
$count_key = 'wp_post_views_count';
$count = get_post_meta($post_id, $count_key, true);

if($count == '') {
$count = 0;
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
} else {
$count++;
update_post_meta($post_id, $count_key, $count);
}
}

function theplus_track_post_view ($post_id) {
if ( !is_single() )
return;

if ( empty ( $post_id) ) {
global $post;
$post_id = $post->ID;
}

theplus_set_post_view($post_id);
}
add_action( 'wp_head', 'theplus_track_post_view');

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

function Your_Custom_Function($query_args) {
$extra_query = array(
'post_type' => 'post', // here you need to write post type
'meta_key' => 'wp_post_views_count', // set custom meta key
'orderby' => 'meta_value_num', // order by

);
$query_args = array_merge($query_args,$extra_query);
return $query_args;
}

add_filter('Custom_Function_Used_In_As_Query_Id', 'Your_Custom_Function');
`

Here our custom query id is *Custom_Function_Used_In_As_Query_Id*.

2. After that add the Dynamic Listing widget to the page. 

3. Then select **Custom Query** from the **Post Listing Types** dropdown.

4. In the **Query ID** field, add your custom query id.

![](https://theplusaddons.com/wp-content/uploads/2023/08/dynamic-listing-blog-posts-based-on-custom-query-new.png)

5. After that, select the appropriate style and layout from the **Style** and **Layout** sections, respectively.

Now your posts will automatically arrange based on the number of views.

Also, read [How to Create Elementor Loop with Custom Post Query](https://theplusaddons.com/docs/create-elementor-loop-with-custom-post-query/).

## Frequently Asked Questions

**Q: What should I do if my custom query isn't showing any posts?**
A: If your custom query isn't displaying posts, first check your query ID in the Dynamic Listing widget. Ensure it matches the ID defined in your custom function. Additionally, verify that your custom query code is correctly written and placed in the functions.php file of your child theme. If youu2019re using a theme like Nexter, confirm that the custom code is properly integrated there.

**Q: What is the best way to order posts by views in Elementor?**
A: To order posts by views, you need to write a custom query that utilizes the 'wp_post_views_count' meta key. This involves adding a function in your functions.php file that tracks post views and modifies the query arguments to order by this meta value. This method ensures that your posts are displayed in order of popularity.

**Q: Are there any limitations when using custom queries in Elementor?**
A: Custom queries in Elementor require some familiarity with PHP coding, as they involve writing custom functions. This might be a limitation for users who are not comfortable with coding. Additionally, the Custom Query option is intended for advanced users, so beginners may find it challenging to implement effectively.

**Q: How do I ensure my custom code remains intact during theme updates?**
A: To keep your custom code safe during theme updates, always use a child theme for modifications. By adding your custom functions to the functions.php file of the child theme, you ensure that your changes are preserved even when the parent theme is updated. This practice is crucial for maintaining custom functionality.
