1

I want on my homepage on my first page to be displayed only featured listings no matter how old they are

This is my homepage code:

   <?php get_header(); ?>

<?php
if(get_option('aven_home') == "listing") { ?>   
<?php include (TEMPLATEPATH . '/lib/listhome.php'); ?>
<?php } else { ?>
<div id="content">

<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>   

<div class="post" id="post-<?php the_ID(); ?>">

<div class="title">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
</div>
<div class="postmeta">
    <span class="author">Posted by <?php the_author(); ?> </span> <span class="clock">  <?php the_time('M - j - Y'); ?></span> <span class="comm"><?php comments_popup_link('0 Comment', '1 Comment', '% Comments'); ?></span>
</div>

<div class="entry">

<?php
if ( has_post_thumbnail() ) { ?>
    <a href="<?php the_permalink() ?>"><img class="postimg" src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php get_image_url(); ?>&amp;h=150&amp;w=200&amp;zc=1" alt=""/></a>
<?php } else { ?>
    <a href="<?php the_permalink() ?>"><img class="postimg" src="<?php bloginfo('template_directory'); ?>/images/dummy.png" alt="" /></a>
<?php } ?>
<?php wpe_excerpt('wpe_excerptlength_index', ''); ?>
<div class="clear"></div>
</div>

</div>

<?php endwhile; ?>

<div class="clear"></div>

<?php getpagenavi(); ?>

<?php $wp_query = null; $wp_query = $temp;?>

</div>
<?php } ?>
<?php get_sidebar(); ?>

<?php get_footer(); ?>
0

First, I recomend you to use pre_get_post hook for your purpose. If you are going to use only the new WP_Query in your theme, it has no sense that Wordpress run a query before it gets your theme becuause it would be a extra work that will be discarded. Using pre_get_posts we can alter the main query to fit our purpose and get what we want without executing another query.

Thats said. Here an example code:

//Functions for filters
add_action( 'pre_get_posts', 'properties_pre_get_post' );
function properties_pre_get_post($query){

    //limit to frontend, to the main query and to home page
   if($query->is_main_query() && !is_admin() && is_home() ) {
        //the main query to get only sticky posts
        $query->set('post__in',get_option( 'sticky_posts' ));
    }

}

Put that code in functions.php and in your home.php template file you can run the loop as usual.

Custom post types has not support for built-in 'sticky' feature but you can create, for example, a tag or taxonomy term and filter by this tag. For example, if you custom post type support post_tags taxonomy you can created a term called 'featured' and attach each post you want to this tag and filter:

//Functions for filters
add_action( 'pre_get_posts', 'my_pre_get_post' );
function my_pre_get_post($query){

     //limit to main query, frontend and home page
     if($query->is_main_query() && !is_admin() && is_home() ) {
          $tax_query = array (
                             'taxonomy'=> array('post_tags'),
                             'field'   => 'slug',
                             'terms'   => 'featured',
                             );
          $query->set('tax_query',$tax_query);
          //filter also by your custom post type
          $query->set('post_type','listings');
      } 
}
  • i don't have the option to make my post stiky. it will work ? The template i am using has a custom taxonomy made listing type and i have 3 option here sold reduce and featured, what i want is the featured to be the first – Bogdan Bătrânu Sep 6 '13 at 11:50
  • Are you using a custom post type? – cybmeta Sep 6 '13 at 11:53
  • yes. the custom post type is called listings – Bogdan Bătrânu Sep 6 '13 at 11:55
  • The custom post types have not sticky feature as normal posts have. You can get a similar functionality adding, for example, a toxonomy term. But then the code I posted have to change to perform a taxonomy query. – cybmeta Sep 6 '13 at 11:58
  • Please, see edited answer – cybmeta Sep 6 '13 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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