0

Hi I this weird problem where i have my custom front-page.php with set page (in Reading Settings) static page.

What I want to have is a static page with welcoming message and later 1 recent posts from post-type and 4 recent posts form custom post type.

To get static page info I use:

<?php while ( have_posts() ) : the_post(); ?>
  <?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>

Then to get 1 recent posts I use:

<?php 
   $args = array( 'numberposts' => '1', 'meta_key'=>'_thumbnail_id' );
   $recent_posts = wp_get_recent_posts( $args );
   foreach($recent_posts as $post) : ?>
      <?php get_template_part( 'content', '' ); ?>
<?php endforeach; ?>

And finally, to load 4 custom posts:

<?php 
   $getPorftolio = new WP_Query( 'post_type=portfolio&posts_per_page=4' );
   if ($getPorftolio->have_posts()) {
      while ($getPorftolio->have_posts()) {
        $getPorftolio->the_post();

        get_template_part( 'content', 'portfolio-index' );
      }
} ?>

Of course there are additional mark-up and content-_ pages work just fine (separately work well).

Problem is when it loads, the POST type post (where loads only 1) the info is mixed up. It loads correct title, correct featured image, but categories, tags, meta is empty and excerpt is from static page. As if it mixes where it should load static page info and post info. The biggest mistery - the same page works completely fine on my local WAMP.

The test server: http://www.appuilab.com/sandbox/nerijusgood/

EDIT: I also added , which did not help at all. Also I have noticed that my tags/categories/post-meta is messed up on my blog (home.php) as well. So I figure maybe the problem is in the template part content.php (in both places this one is loaded). Meta is loaded with the same function in each page (in others it works just fine).

Any ideas? I am still bummed ;/

1

You have to reset every instance of a custom query, otherwise you will get unexpected output from any other query there after

Simply use wp_reset_postdata() after every custom query.

Example

<?php 
   $args = array( 'numberposts' => '1', 'meta_key'=>'_thumbnail_id' );
   $recent_posts = wp_get_recent_posts( $args );
   foreach($recent_posts as $post) : ?>
      <?php get_template_part( 'content', '' ); ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

//NEXT CUSTOM QUERY

EDIT 1

Why not use WP_Query as well for your wp_get_recent_posts query. Here is a bump up of all your code together. You just need to make sure that your meta_key is correct

<?php while ( have_posts() ) : the_post(); ?>
  <?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>

<?php 
$args = array( 'posts_per_page' => '1', 'meta_key'=>'_thumbnail_id' );
$recent_posts = new WP_Query( $args ); ?>

<?php while ( $recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
    <?php get_template_part( 'content' ); ?>
<?php endwhile; // end of the loop. ?>
<?php wp_reset_postdata(); ?>

<?php 
   $getPorftolio = new WP_Query( 'post_type=portfolio&posts_per_page=4' );
   if ($getPorftolio->have_posts()) {
      while ($getPorftolio->have_posts()) {
        $getPorftolio->the_post();

        get_template_part( 'content', 'portfolio-index' );
      }
} 
wp_reset_postdata();
?>

Note: if your template is called content.php, just call it as get_template_part( 'content' );

  • See my update. Hope this helps – Pieter Goosen Aug 20 '14 at 14:12
  • 1
    Hello, this is really helpful, I cleaned up my code and made it consistent. Everything seems to work good. – nerijusgood Aug 20 '14 at 17:46

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.