0

My WordPress website is now redirected to latest post by adding below function to function.php

add_action( 'template_redirect', 'redirect' );
function redirect() {
    $args = array(
    'numberposts' => 1,
    'post_type' => 'seller',
    'post_status' => 'publish');
    $last = wp_get_recent_posts($args);
$last_id = $last['0']['ID'];
        if ( is_home() && ! is_paged() && ! is_archive() && ! is_tag() && !isset($_GET['ptype'])  ) :
             wp_redirect( get_permalink($last_id) , 301 ); 

                exit;
        endif;
}

now, I want this feature but without changing URL to latest post! I mean when you go to http://www.example.com latest post shows without a 301 redirect to http://www.example.com/article/some-other-texts

2 Answers 2

1

Custom query loops and custom page templates are your friends.

Ditch the redirect function and on your home page template do something like this:

<?php
$args = array(
    'post_status' => 'publish',
    'post_type' => 'seller',
    'posts_per_page' => 1
);

query_posts( $args );

// the Loop
while (have_posts()) : the_post();
    the_content(  );
endwhile;
?>

You can find all args here

0
2

You can hook into parse_query and modify the wp_query object for the home page.

First, hook into parse_query, your hooked function will get the wp_query object as it's only argument. It's a reference, so you can change it in place. No return value necessary.

Inside your function, you can check to see if it's the main query (using the new is_main_query function) and if it's the home page. From there, change the query variables!

<?php
add_filter( 'parse_query', 'wpse41420_one_post' );
function wpse41420_one_post( $query )
{
    // make sure we're modifying the main query on the home page
    if( ! $query->is_main_query() || ! is_home() ) return;

    // Set up one post per page
    $query->query_vars['posts_per_page'] = 1;

    // ignore stick posts
    $query->query_vars['ignore_sticky_posts'] = 1;
}

As a plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.