Jump to content

Choose Posts Order in Homepage Display


shir

Recommended Posts

I have 3 sections In my WP theme in the display of the homepage:
"main story", "editor's pick" and "trending stories".
Each one of my latest 2 posts displayed several times in the homepage in different sections (!): under "main story", under "editor's pick" and under "trending stories".

Instead, I want all the 5 latest posts will display on the homepage, but each of them will appear once only.
(For example: under main story - post 1, under trending - posts 2+3, under editor's pick - posts 4+5).

How can I do this?

Attached screenshot of my homepage

newscard homepage.PNG

Edited by shir
Link to comment
Share on other sites

It's indeed determined by the time the post was published (=latest posts).
However, I'm looking for the code who'd say; let's publish the latest 5 posts in the 3 sections - while each one would appear once.
Meaning, avoid post duplicated display
, as well as determine the post order, as I mentioned above (latest under "main story", the 2 before - under "trending stories", the 2 before - under "editor's pick".

I got one code to prevent duplication. But it made the posts display disappeared from the top 3 sections completely, and leave display of latest post only in the sections below the 3 top sections.

The code was (if that helps): + attached screenshot after implanting the code
 

add_action('parse_query', 'mts_exclude_duplicates');

function mts_exclude_duplicates( &$query ) {

if (!is_home()) return;

global $adp_posts;

$disable_now = $query->get('adp_disable'); // use 'adp_disable' to prevent exclusion

if (empty($query->post__not_in) && empty($disable_now)) {

$query->set('post__not_in', $adp_posts);

}

}

 

add_filter('the_posts', 'mts_log_posts', 10, 2);

$adp_posts = array();

function mts_log_posts( $posts, $query ) {

if (!is_home()) return $posts;

global $adp_posts;

foreach ($posts as $i => $post) {

$adp_posts[] = $post->ID;

}

return $posts;

}

hp after implanting code.PNG

Edited by shir
Link to comment
Share on other sites

Yes but! If one shows latest published, one shows most viewed which could be the pages/posts you are working currently on and viewing, it WILL show duplicate, because you haven't enough views from others to increase views on other posts. By hiding duplicates for so few posts, and views which it is supposed to do, you are overriding its default functionality. Also using this code to not display duplicates, well surely the result is exactly you would expect? If it prevented duplicate to show alternative other posts, that would be what you want. You can change publish date, view history try adjusting these for different posts. If this plugin? has setting to filter posts from these four, adjust those.

This plugin seems to allow you do exactly that

https://wordpress.org/plugins/wordpress-popular-posts/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...