Archived topic
Create a Most Popular or Most Viewed Posts section on Cover Page
23 replies · Started by Evonne on March 30, 2021
Hello,
Theme template: Dispatch
I have been searching for 2 days to see if there was anything that can help me create a "Most Popular" posts of all time just below the Header element. I liked that the Header element already displays the latest posts. I am already using WP Show Posts but it doesn't sort based on page views and I can't figure out how Elements work...
I don't really want to look for another plugin just for this.
Hi there,
you can simply add another WP Show Posts list to either the home page content.
In this topic here, Elvin provides the shortcode method ( and a filter ) for ordering the list based on post_views_count
I got a syntax error, unexpected token "=>" on this row:
$args['meta_query'] => array(
Hi There,
did you copy Elvins code exactly?
As that could be related to a syntax issue, such as curly quotes instead of ' straight quotes in the preceeding argument
Yes i copied it. It looks like this:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
if ( 123 === $settings['list_id'] ) {
$args['meta_query'] => array(
array(
'key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
)
);
}
return $args;
} );
Hi Morgan,
Ah right there's actually a syntax issue there as you've pointed. (I've corrected the one from WPSP now)
Let's correct this one as well.
Try this:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
if ( 123 === $settings['list_id'] ) {
$args['meta_query'] = array(
array(
'key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
)
);
}
return $args;
} );
Reminder: You'll also have to make this change in your wp-show-posts.php file: https://github.com/tomusborne/wp-show-posts/commit/286caf1164db8b6b6f38b85d3a011b519a27f4de - for the filter to work properly. (You can skip this if you're using the beta version - https://wpshowposts.com/wp-show-posts-1-2-0/)
Also make sure to change 123 to the WPSP list ID you're going to use this on. :)
Hi Elvin,
No i get his:
Uncaught ArgumentCountError: Too few arguments to function {closure}(), 1 passed in wp-includes/class-wp-hook.php on line 307 and exactly 2 expected in wp-content/themes/generatepress_child/functions.php:24
Stack trace:
#0 wp-includes/class-wp-hook.php(307): {closure}()
#1 wp-includes/plugin.php(189): WP_Hook->apply_filters()
#2 wp-content/plugins/wp-show-posts/wp-show-posts.php(383): apply_filters()
#3 wp-content/plugins/wp-show-posts/wp-show-posts.php(569): wpsp_display()
#4 wp-includes/shortcodes.php(356): wpsp_shortcode_function()
#5 [internal function]: do_shortcode_tag()
#6 wp-includes/shortcodes.php(228): preg_replace_callback()
#7 wp-includes/class-wp-hook.php(307): do_shortcode()
#8 wp-includes/plugin.php(189): WP_Hook->apply_filters()
#9 wp-includes/post-template.php(253): apply_filters()
#10 wp-content/themes/generatepress/content-page.php(73): the_content()
#11 wp-includes/template.php(772): require('/home/718213.cl...')
#12 wp-includes/template.php(716): load_template()
#13 wp-includes/general-template.php(204): locate_template()
#14 wp-content/themes/generatepress/inc/theme-functions.php(568): get_template_part()
#15 wp-content/themes/generatepress/page.php(34): generate_do_template_part()
#16 wp-includes/template-loader.php(106): include('/home/718213.cl...')
#17 wp-blog-header.php(19): require_once('/home/718213.cl...')
#18 index.php(17): require('/home/718213.cl...')
#19 {main}
thrown
Were you able to do the required code edits on WPSP's wp-show-posts.php file? it's important to make this code snippet work. :D
I added this to wp-ashow-posts.php:
$query = new WP_Query( apply_filters( 'wp_show_posts_shortcode_args', $args ) );
$query = new WP_Query( apply_filters( 'wp_show_posts_shortcode_args', $args, $settings ) );<code></code>
And this to function file:
add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
if ( 123 === $settings['list_id'] ) {
$args['meta_query'] = array(
array(
'key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
)
);
}
return $args;
} );
Dont know what im doing wrong here but i still gets this:
Uncaught ArgumentCountError: Too few arguments to function {closure}(), 1 passed in wp-includes/class-wp-hook.php on line 307 and exactly 2 expected in wp-content/themes/generatepress_child/functions.php:20
Stack trace:
#0 wp-includes/class-wp-hook.php(307): {closure}()
#1 wp-includes/plugin.php(189): WP_Hook->apply_filters()
#2 wp-content/plugins/wp-show-posts/wp-show-posts.php(383): apply_filters()
#3 wp-content/plugins/wp-show-posts/wp-show-posts.php(570): wpsp_display()
#4 wp-includes/shortcodes.php(356): wpsp_shortcode_function()
#5 [internal function]: do_shortcode_tag()
#6 wp-includes/shortcodes.php(228): preg_replace_callback()
#7 wp-includes/class-wp-hook.php(307): do_shortcode()
#8 wp-includes/plugin.php(189): WP_Hook->apply_filters()
#9 wp-includes/post-template.php(253): apply_filters()
#10 wp-content/themes/generatepress/content-page.php(73): the_content()
#11 wp-includes/template.php(772): require('/home/718213.cl...')
#12 wp-includes/template.php(716): load_template()
#13 wp-includes/general-template.php(204): locate_template()
#14 wp-content/themes/generatepress/inc/theme-functions.php(568): get_template_part()
#15 wp-content/themes/generatepress/page.php(34): generate_do_template_part()
#16 wp-includes/template-loader.php(106): include('/home/718213.cl...')
#17 wp-blog-header.php(19): require_once('/home/718213.cl...')
#18 index.php(17): require('/home/718213.cl...')
#19 {main}
thrown
Ah that's the issue.
This line:
$query = new WP_Query( apply_filters( 'wp_show_posts_shortcode_args', $args ) );
Has to be replaced with this line:
$query = new WP_Query( apply_filters( 'wp_show_posts_shortcode_args', $args, $settings ) );
We need the $settings passed to the filter so we can target a specific list. :)
Its not working for me. Now i get this:
Dina ändringar i PHP-kod har återställts till tidigare version på grund av ett fel på rad 24 i filen wp-content/themes/generatepress_child/functions.php. Korrigera och försök spara en gång till.
Uncaught ArgumentCountError: Too few arguments to function {closure}(), 1 passed in wp-includes/class-wp-hook.php on line 309 and exactly 2 expected in wp-content/themes/generatepress_child/functions.php:24
Stack trace:
#0 wp-includes/class-wp-hook.php(309): {closure}()
#1 wp-includes/plugin.php(189): WP_Hook->apply_filters()
#2 wp-content/plugins/wp-show-posts/wp-show-posts.php(383): apply_filters()
#3 wp-content/plugins/wp-show-posts/wp-show-posts.php(569): wpsp_display()
#4 wp-includes/shortcodes.php(356): wpsp_shortcode_function()
#5 [internal function]: do_shortcode_tag()
#6 wp-includes/shortcodes.php(228): preg_replace_callback()
#7 wp-includes/class-wp-hook.php(307): do_shortcode()
#8 wp-includes/plugin.php(189): WP_Hook->apply_filters()
#9 wp-includes/post-template.php(253): apply_filters()
#10 wp-content/themes/generatepress/content-page.php(73): the_content()
#11 wp-includes/template.php(772): require('/home/718213.cl...')
#12 wp-includes/template.php(716): load_template()
#13 wp-includes/general-template.php(204): locate_template()
#14 wp-content/themes/generatepress/inc/theme-functions.php(568): get_template_part()
#15 wp-content/themes/generatepress/page.php(34): generate_do_template_part()
#16 wp-includes/template-loader.php(106): include('/home/718213.cl...')
#17 wp-blog-header.php(19): require_once('/home/718213.cl...')
#18 index.php(17): require('/home/718213.cl...')
#19 {main}
thrown
The error is still saying the change to the plugin's code wasn't applied (or applied properly). Perhaps I wasn't clear. My apologies.
Any change you can copy the site to a staging site or let us have temporary backend access to the site you're working on to check if it's applied properly?
You can use the private information text field to provide the details -
https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
Ok,
I'll fix that. No problem it's a staging site. I have sent you the credentials now.
I’ll fix that. No problem it’s a staging site. I have sent you the credentials now.
did you send it through our contact page? The system has yet to receive it.
I just realized you're not the topic starter of this thread. lol.
Can you open a new one? So you can use the private information text field -
https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
I opened a new support topic as i dont have the private field in this.