• Resolved salihenesalkan

    (@salihenesalkan)


    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.

    But I couldn’t do the same things on my own theme. I tried to do it by following the steps in this link 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

    (@hcabrera)

    Hi @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) 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.)

    Thread Starter salihenesalkan

    (@salihenesalkan)

    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

    (@hcabrera)

    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.