Hi @kennnesbitt,
thank you for sharing this! I believe this will be very useful for users of the plugin. I’m also glad to hear that you like the plugin 🙂
Thanks for your support!
Regards,
Blaz
My pleasure. Feel free to add this to your documentation if you think it’s appropriate.
Cheers,
Kenn
Thanks Kenn! I added to the documentation. It would be great if more people would share such useful snippets/tutorials 🙂
Regards,
Blaz
Thanks for sharing @kennnesbitt – would this simply be entered in functions.php?
I’m using custom posts too under kktfwp_portfolio
Here is my example page: https://ginratings.com.au/
Currently the only gin that has reviews is “Threefold Aromatic Gin”.
Cheers,
Steve
Hi @stevethebartender – Your site looks great!
I used this code on two different websites, but I don’t use it on my home page like you are doing.
On https://www.giggleverse.com/ I put it in a page template called page-template_sort-by-rating.php. Then I just created a blank page and applied that template to it.
On the https://www.poetry4kids.com/ I put it in the archive pages for each of my custom post types (e.g., archive-poem.php, archive-nursery_rhyme.php, etc.). Or, rather, I put the code in a separate custom-poem-sort.php file and then included that file in each of the custom post type archives.
I hope that helps.
Sorry @kennnesbitt – I completely missed your reply until now.. thanks for running through the details. Unfortunately I couldn’t get it to work (page template). Any chance you can share the full page template code?
Would love to be able to use this feature 🙂
Cheers,
STeve
Hi @stevethebartender – Here is the full page template code from GiggleVerse. The template itself is called page-template_sort-poems-by-rating.php. The four lines of code that are commented out are what I was using to debug the template. I left them in here in case they might come in handy for your testing.
<?php
/*
* Template Name: Sort Poems by Rating
* Template Post Type: page
*/
get_header();
?>
<div class="content">
<div class="row">
<main class="column large-8 medium-8 small-12">
<div class="gv-topic-list">
<h1>Most Popular Poems</h1>
<p>Here are the most popular poems on GiggleVerse, as rated by you!
These may change as more poems are added to the site and more people rate them,
so keep on reading and voting for your favorites!</p>
<?php
$colors = array('red', 'blue', 'purple', 'green', 'slate', 'orange', 'concrete');
$i = 0;
add_filter('posts_orderby', 'edit_posts_orderby');
add_filter('posts_join_paged','edit_posts_join_paged');
add_filter( 'posts_groupby', 'edit_posts_groupby' );
function edit_posts_groupby($groupby) {
global $wpdb;
$analytics = $wpdb->prefix . "rmp_analytics";
$groupby = "$analytics.post";
return $groupby;
}
function edit_posts_join_paged($join_paged_statement) {
global $wpdb;
$analytics = $wpdb->prefix . "rmp_analytics";
$join_paged_statement .= "LEFT JOIN $analytics ON $analytics.post = " . $wpdb->prefix . "posts.ID";
return $join_paged_statement;
}
function edit_posts_orderby($orderby_statement) {
global $wpdb;
$analytics = $wpdb->prefix . "rmp_analytics";
$orderby_statement = "(SUM($analytics.value)/COUNT($analytics.votes)) DESC";
return $orderby_statement;
}
$paged = get_query_var( 'paged' );
$args = array(
'post_type' => 'gv_poem',
'post_status' => 'publish',
'posts_per_page' => 24,
'paged' => $paged,
);
$query = new WP_Query($args);
//echo "<br />Records returned: " . $query->found_posts . "<br />";
//echo "<br />Post Count: " . $query->post_count . "<br />";
//echo "Last Error: " . $wpdb->last_error . "<br />";
//echo "Last Query: " . $wpdb->last_query . "<br />";
?>
<ul>
<?php
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$icon = get_field('poemicon');
$author = get_the_terms($post->ID, 'author');
if ($i == count($colors)) { $colors = array('red', 'blue', 'purple', 'green', 'slate', 'orange', 'concrete'); $i = 0; } ?>
<li class="<?php echo $colors[$i]; ?>">
<?php if ($icon) { ?>
<img class="topic-icon" src="<?php echo $icon; ?>">
<?php } ?>
<h3><?php echo get_the_title(); ?></h3>
<?php if ($author) { ?>
<div class="gv-poem-author">by <?php echo $author[0]->name; ?></div>
<?php } ?>
<div class="gv-view-poem">View Poem ⟶</div>
<a href="<?php echo get_the_permalink();?>"></a>
</li>
<?php $i++;
endwhile;
endif; ?>
</ul>
<?php
if (function_exists("pagination")) {
pagination($query->max_num_pages);
}
wp_reset_postdata();
?>
</div>
</main>
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>