Title: Custom query problem
Last modified: October 24, 2021

---

# Custom query problem

 *  Resolved [salihenesalkan](https://wordpress.org/support/users/salihenesalkan/)
 * (@salihenesalkan)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/custom-query-problem-2/)
 * Hello, I want to make a post listing on my theme using the plugin. Basically 
   what we want to do has been shared in the last 24 hours
    and listing the posts
   with the most views. I was able to list on Elementor by following the instructions
   [in this article.](https://wordpress.org/support/topic/how-to-query-wpp-with-elementor/)
 * But I couldn’t do the same things on my own theme. I tried to do it by following
   the steps [in this link](https://wordpress.org/support/topic/how-to-sorting-a-custom-query-by-views-all-time-monthly-weekly-or-daily/)
   but I don’t know what to put where so i haven’t succeed.
 * The theme adds its own features and instead of using the Elementor Query tab,
   it has added its own “Post Source” field, and from here only i can list queries
   the inside of the theme.
 * . The queries are added to the theme file as follows.
 * loop-options.php
 *     ```
       'sort_by'  => [
       				'label'       => esc_html__('Sort By', 'bunyad-admin'),
       				'type'        => 'select',
       				'options'     => [
       					''              => esc_html__('Published Date', 'bunyad-admin'),
       					'modified'      => esc_html__('Modified Date', 'bunyad-admin'),
       					'random'        => esc_html__('Random', 'bunyad-admin'),
       					'comments'      => esc_html__('Comments Count', 'bunyad-admin'),
       					'alphabetical'  => esc_html__('Alphabetical', 'bunyad-admin'),
       					'rating'        => esc_html__('Rating', 'bunyad-admin'),
   
       				] + (
       					// Not using global setting for now.
       					class_exists('\Jetpack') && \Jetpack::is_module_active('stats')
       						? ['jetpack_views' => esc_html__('JetPack Plugin Views Count', 'bunyad-admin')]
       						: []
       				),
       				'condition' => ['query_type' => 'custom'],
       			],
   
       			'sort_days'  => [
       				'label'       => esc_html__('Sort Days', 'bunyad-admin'),
       				'description' => esc_html__('Number of days to use for the views sort. Max limit is 90 days for Jetpack.', 'bunyad-admin'),
       				'type'        => 'number',
       				'default'     => 30,
       				'condition'   => [
       					'sortby' => 'jetpack_views',
       					'query_type' => 'custom'
       				]
       			],
       ```
   
 * query.php
 *     ```
       /**
       		 * Sortng criteria
       		 */
       		switch ($sort_by) {
       			case 'modified':
       				$query_args['orderby'] = 'modified';
       				break;
   
       			case 'random':
       				$query_args['orderby'] = 'rand';
       				break;
   
       			case 'comments':
       				$query_args['orderby'] = 'comment_count';
       				break;
   
       			case 'alphabetical':
       				$query_args['orderby'] = 'title';
       				break;		
   
       			case 'rating':
       				$query_args = array_replace(
       					$query_args, 
       					[
       						'meta_key' => '_bunyad_review_overall', 
       						'orderby' => 'meta_value_num'
       					]
       				);
       				break;
   
   
   
       		}
       ```
   
 * How can I add WPP plugin queries here and make it list.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/custom-query-problem-2/#post-15001683)
 * Hi [@salihenesalkan](https://wordpress.org/support/users/salihenesalkan/),
 * I’m not sure if this will work since I don’t know how your theme works but give
   it a try and let me know how it goes:
 * **1.** Add the code from step 1 on [How To: Sorting a custom query by views (All time, monthly, weekly, or daily)](https://wordpress.org/support/topic/how-to-sorting-a-custom-query-by-views-all-time-monthly-weekly-or-daily/)
   to your theme’s _functions.php_ file, then follow the instructions from step 
   3.
 * **2.** Change your code from _loop-options.php_ to this:
 *     ```
       'sort_by'  => [
           'label'       => esc_html__('Sort By', 'bunyad-admin'),
           'type'        => 'select',
           'options'     => [
               ''              => esc_html__('Published Date', 'bunyad-admin'),
               'modified'      => esc_html__('Modified Date', 'bunyad-admin'),
               'random'        => esc_html__('Random', 'bunyad-admin'),
               'comments'      => esc_html__('Comments Count', 'bunyad-admin'),
               'alphabetical'  => esc_html__('Alphabetical', 'bunyad-admin'),
               'rating'        => esc_html__('Rating', 'bunyad-admin'),
               'wpp_views'     => esc_html__('WordPress Popular Posts Views Count', 'bunyad-admin'),
           ] + (
               // Not using global setting for now.
               class_exists('\Jetpack') && \Jetpack::is_module_active('stats')
                   ? ['jetpack_views' => esc_html__('JetPack Plugin Views Count', 'bunyad-admin')]
                   : []
           ),
           'condition' => ['query_type' => 'custom'],
       ],
   
       'sort_days'  => [
           'label'       => esc_html__('Sort Days', 'bunyad-admin'),
           'description' => esc_html__('Number of days to use for the views sort. Max limit is 90 days for Jetpack.', 'bunyad-admin'),
           'type'        => 'number',
           'default'     => 30,
           'condition'   => [
               'sortby' => 'jetpack_views',
               'query_type' => 'custom'
           ]
       ],
       ```
   
 * **3.** Change your code from _query.php_ to this:
 *     ```
       /**
        * Sortng criteria
        */
       switch ($sort_by) {
           case 'modified':
               $query_args['orderby'] = 'modified';
               break;
   
           case 'random':
               $query_args['orderby'] = 'rand';
               break;
   
           case 'comments':
               $query_args['orderby'] = 'comment_count';
               break;
   
           case 'alphabetical':
               $query_args['orderby'] = 'title';
               break;		
   
           case 'rating':
               $query_args = array_replace(
                   $query_args, 
                   [
                       'meta_key' => '_bunyad_review_overall', 
                       'orderby' => 'meta_value_num'
                   ]
               );
               break;
   
           case 'wpp_views':
               $query_args = array_replace(
                   $query_args, 
                   [
                       'meta_key' => 'views_daily', 
                       'orderby' => 'meta_value_num'
                   ]
               );
               break;
   
       }
       ```
   
 * (Change _views\_daily_ to whichever meta key you prefer from the ones listed 
   [here](https://wordpress.org/support/topic/how-to-sorting-a-custom-query-by-views-all-time-monthly-weekly-or-daily/).)
 *  Thread Starter [salihenesalkan](https://wordpress.org/support/users/salihenesalkan/)
 * (@salihenesalkan)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/custom-query-problem-2/#post-15003649)
 * Thanks! It’s worked. I really appreciated. Now all i need to add is to list what
   was published in the same time period i choose (views_daily). How can I do that?
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/custom-query-problem-2/#post-15003865)
 * No idea to be honest.
 * Sorting by daily views would probably need some extra coding, I don’t think it’s
   something you can do by just editing some arrays as we did above. This probably
   needs a custom “condition” as seen on query.php like the one for Jetpack – only
   this time it’d be a custom condition for WPP. Can’t say what “condition” to use(
   or even if that’s the way to do it) because I don’t know how your theme works(
   don’t even know which theme you’re using haha).
 * It might be a good idea to reach out to your theme developer and ask them about
   this.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Custom query problem’ is closed to new replies.

 * ![](https://ps.w.org/wordpress-popular-posts/assets/icon-256x256.png?rev=1232659)
 * [WP Popular Posts](https://wordpress.org/plugins/wordpress-popular-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wordpress-popular-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wordpress-popular-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wordpress-popular-posts/reviews/)

## Tags

 * [query](https://wordpress.org/support/topic-tag/query/)

 * 3 replies
 * 2 participants
 * Last reply from: [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * Last activity: [4 years, 7 months ago](https://wordpress.org/support/topic/custom-query-problem-2/#post-15003865)
 * Status: resolved