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.

I'm working on a template which will use some custom-post-types & custom-taxonomies.. I'm having problems with pagination and duplicate posts.

HOME.PHP

LOOP #1 On the homepage (home.php) I have 2 Loops. The fist one shows 1 post (custom post type: Projects) which has the custom-taxonomy "Featured" assigned to it.

LOOP #2 On the second loop I have the six most recent "Projects" (Custom post type) but I dont want a duplicate from the first loop. Under the six posts I'd like a pagination

Both loops in home.php work.. but I still have that duplicate and I can't get pagination to work..

PasteBin

  • home.php: http://pastebin.com/6ac2asue

  • functions.php: http://pastebin.com/1SK206Bh

    <?php $do_not_duplicate = array();
    $folio_loop = new WP_Query( array
        (
            'featured' => 'featured-post',
            'post_type' => 'projects',
            'posts_per_page' => '1',
            )
        );
    
         while ( $folio_loop->have_posts() ) : $folio_loop->the_post();
        $do_not_duplicate[] = $post->ID; ?>
    
        <div id="featured" style="position: relative;">
    
    
            <div class="featured-overlay" style="position: absolute; right: 20px; bottom: 20px; width: 165px; height: 165px; text-align: center;">
                <div class="project-meta-featured">
              <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
            <span>
            <?php
                global $post;
                $text = get_post_meta( $post->ID, '_cmb_project_meta', true );
                echo $text;
            ?>
            </span>
            <br />
            <?php
                global $post;
                $text = get_post_meta( $post->ID, '_cmb_client_meta', true );
                echo $text;
            ?>
    
    
            </a>
              </div>
            </div>
    
            <?php if ( has_post_thumbnail()) : ?>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                    <?php the_post_thumbnail('featured'); ?>
                </a>
            <?php endif; ?>
    
    
    
        </div>
    
    <?php endwhile; ?>
    
share|improve this question
    
- featured.php: pastebin.com/2X8xTPTY –  Eamon Oct 19 '12 at 18:43
1  
Pleas don't use pastebins. They can get lost. Always reduce it to the necessary part of your question and add this code directly in it. –  kaiser Oct 19 '12 at 20:31
    
your $do_not_duplicate variable is not global so it cannot be shared through different php files. You create it in feature.php and then use it in home.php. The issue is the same for pagination : you are using the global $wp_query instead of the one you create in home.php –  Fabien Quatravaux Oct 20 '12 at 1:12

1 Answer 1

I think the best way to implement this is to use pre_get_posts hook. Take a look at this code

function customize_query( $query ) {
    $post = get_posts(array(
         'post_type' => 'projects',
         'taxonomy' => 'featured',
         'numberposts' => 1
    ));
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', 'projects' );
        $query->set( 'posts_per_page', 6 );
        if($post && !empty($post))
            $query->set( 'post__not_in', array($post[0]->ID) );
    }
}
add_action( 'pre_get_posts', 'customize_query' );

Then in your home page

$post = get_posts(array(
     'post_type' => 'projects',
     'taxonomy' => 'featured',
     'numberposts' => 1
));
// display the post just retrieved, it must be coming from cache

// we can use the global query since now it contains the 6 posts we want
while(have_posts()) :
    the_post();
    // display the post
endwhile;

You'll need to modify it a lot but it might help you achieve what you want. In here the pagination should wotrk as normal since you just modified the main page query

share|improve this answer
    
I tried to implement some parts of your code: <code>function custom_query( $query ) { if ( is_home()&& is_main_query() ) { //Display 6 projects in home $query->query_vars['posts_per_page'] = 6; $query->set( 'post_type', 'projects' ); return; } } add_action('pre_get_posts', 'custom_query', 1);</code> It gives me 6 posts and pagination works. BUT I loose my navigation (really why??). If I paste the code for the featured post in functions.php my apache server crashes (again.. why?).. –  Eamon Oct 20 '12 at 13:38
    
you can't use is_home()&& is_main_query(), it's $query->is_home() && $query->is_main_query(). & this code can't make server crash, it must be something else –  Mridul Aggarwal Oct 20 '12 at 13:43
    
Ah yes that solved my navigation problem.. I'm running a local server and when I paste $post = get_posts(array( 'post_type' => 'projects', 'taxonomy' => 'featured', 'numberposts' => 1 )); in functions.php and hit save, I immediately get a message that apache stoped working. –  Eamon Oct 20 '12 at 13:50
    
Honestly, i don't see a reason for server crash. Could you provide more details regarding the problem, maybe provide the code present before & after this code? Where you're getting the message? etc. –  Mridul Aggarwal Oct 20 '12 at 13:57
    
no error in the code. Maybe you should see if this server is having problems somewhere else as well or maybe restart it. –  Mridul Aggarwal Oct 20 '12 at 14:31

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.