Modify Blog Sort Order

I am wanting to add some code that displays the posts in the order of the most viewed post first. I have code already added that adds a count to each individual post, and I have the new WP_query for pulling and sorting that, but where within X can I put that in the Pro Child Theme?

Hi @Mokono

If you want to modify the main blog loop, then I believe that’s the file you want to modify:

/pro/framework/views/global/_index.php

First, you need to copy it to the child theme directory, then modify it to suit your need.

Thanks.

Perfect, thanks for the quick reply. I have it all hooked up now!

You are welcome :slight_smile:

Everything worked for displaying the posts, however the pagination broke and while it loads the next page, the top posts are still at the top.

I only adjusted 2 lines of code:

<?php // ============================================================================= // VIEWS/GLOBAL/_INDEX.PHP // ----------------------------------------------------------------------------- // Includes the index output. // ============================================================================= $stack = x_get_stack(); if ( is_home() ) : $style = x_get_option( 'x_blog_style' ); $cols = x_get_option( 'x_blog_masonry_columns' ); $condition = is_home() && $style == 'masonry'; elseif ( is_archive() ) : $style = x_get_option( 'x_archive_style' ); $cols = x_get_option( 'x_archive_masonry_columns' ); $condition = is_archive() && $style == 'masonry'; elseif ( is_search() ) : $condition = false; endif; ?> <?php if ( $condition ) : ?> <?php x_get_view( 'global', '_script', 'isotope-index' ); ?>
<?php if ( have_posts() ) : ?> /// ADJUSTED THE 2 LINES BELOW <?php $popularpost = new WP_Query( array( 'posts_per_page' => 39, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); while ( $popularpost->have_posts() ) : $popularpost->the_post(); ?> <?php if ( $stack != 'ethos' ) : ?> <?php x_get_view( $stack, 'content', get_post_format() ); ?> <?php else : ?> <?php x_ethos_entry_cover( 'main-content' ); ?> <?php endif; ?> <?php endwhile; ?> <?php else : ?> <?php x_get_view( 'global', '_content-none' ); ?> <?php endif; ?>
<?php else : ?> <?php if ( have_posts() ) : ?>
<?php $popularpost = new WP_Query( array( 'posts_per_page' => 39, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'  ) );
                while ( have_posts() ) : the_post(); ?>
  <?php x_get_view( $stack, 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php x_get_view( 'global', '_content-none' ); ?>
<?php endif; ?> <?php endif; ?> <?php pagenavi(); ?>

Is there a better way of adding the ‘Sort By’ instead of writing a new WP_Query? Any help would be greatly appreciated.

Hello There,

If you are modifying the WP_Query, it is best that you do the correct way.
Please check out this link for you reference:

Hope this helps.

This was perfect, thank you for the reference. Your support is always above and beyond!

If you need anything else please let us know.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.