Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer
    
Thanks from answering my stupid question :D :) –  Amino Feb 7 '12 at 18:35

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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