WordPress: Using custom field to define posts to display in loop - wordpress

I'm trying to use a custom field in which I input the post ID numbers of the posts I want to show, seperated by commas. For some reason though, only the first post of the series of the post IDs are displaying. Can someone help? The value of $nlPostIds is (minus the quotes): "1542,1534,1546". Here's the code... the most important part is the 4th line 'post__in' => array($nlPostIds)
<?php
$nlPostIds = get_post_meta($post->ID, 'nlPostIds', true);
$args=array(
'post__in' => array($nlPostIds)
);
query_posts($args);
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="entry">
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="allinfos"><span class="date"><?php the_time('F jS, Y') ?></span> | <span class="comments"><?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?> </span> | <span class="category">Posted in <?php the_category(', ') ?></span> <!-- by <?php the_author() ?> --></div>
<?php the_content('More ยป'); ?>
<?php the_tags('Tags: ', ', ', ' '); ?> <?php edit_post_link('Edit', '[ ', ' ]'); ?>
<div class="clear"></div>
</div></div>
<?php endwhile; endif; ?>
Thanks!

I think you need to also pass the argument 'posts_per_page' as -1 in your $args array (see the Codex on query_posts()).
UPDATE:
Apologies, I've just re-read your question and I think I know the problem. Pass $nlPostIds as the direct argument, without placing it an array. You only pass an array when each element is an ID. In this care you're just passing a comma-separated string.
UPDATE:
Use;
$args = array('post__in' => #explode(',', $nlPostIds), 'posts_per_page' => -1);

Related

WP Query - if/else using custom field

I have a WP query that is looping all my projects with the same code. It's working great, but now I need to change the code based on a custom taxonomy. I basically want to make an if/else loop where if the custom taxonomy contains a certain value, to have x code, and if the custom taxonomy does not contain that certain value, to have y code. Is there a way to add this if/else statement to the "if ( $the_query->have_posts() ) :" section? Or do I need to lay this out differently?
Thank you!
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'projects',
'posts_per_page' => 24,
'paged' => $paged,
);
$the_query = new WP_Query($args);
if( $the_query->have_posts() ) : while ($the_query->have_posts() ) : $the_query->the_post();
?>
EDIT: FluffyKitten (great name) asked for more info so here it is.
My custom taxonomy is called "project_services" and I want all projects with the service "commercial-damages" to loop through with one code, and all other projects to loop through with another code. I tried my crack at it below, but know that my part in the if loop is extremely wrong. I'm really not sure how to add that if condition in there since it's a WP query.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'projects',
'posts_per_page' => 24,
'paged' => $paged,
);
$the_query = new WP_Query($args);
if( $the_query->have_posts() ) : while ($the_query->have_posts() ) : $the_query->the_post();
if (get_field('project_services') == 'commercial-damages'):
?>
<div class="projects-column">
<div class="project-image">
<?php $image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" class="project-image-tester" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<div class="project-info">
<span class="proj-name" style="font-weight: bold;"><b><?php the_title(); ?></b></span>
</div>
<div class="project-onhover">
<span class="proj-title"><p><b><?php the_title(); ?></b></p></span>
<span class="proj-details">
<p><span style="font-weight: bold;">LOCATION: </span><?php the_field('location'); ?></p>
<p><span style="font-weight: bold;">CLAIM VALUE: </span><?php the_field('project_value'); ?></p>
<p><span style="font-weight: bold;">SERVICES: </span> <?php echo wp_strip_all_tags( get_the_term_list( $post->ID, 'project_services', '', ', ', '' ) ); ?> </p></span>
</div><!--projects-on-hover-tester -->
</div><!--project-image-->
</div><!--projects-column -->
<?php else: ?>
<div class="projects-column">
<div class="project-image">
<?php $image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" class="project-image-tester" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<div class="project-info">
<span class="proj-name" style="font-weight: bold;"><b><?php the_title(); ?></b></span>
</div>
<div class="project-onhover">
<span class="proj-title"><p><b><?php the_title(); ?></b></p></span>
<span class="proj-details">
<p><span style="font-weight: bold;">LOCATION: </span><?php the_field('location'); ?></p>
<p><span style="font-weight: bold;">PROJECT VALUE: </span><?php the_field('project_value'); ?></p>
<p><span style="font-weight: bold;">SERVICES: </span> <?php echo wp_strip_all_tags( get_the_term_list( $post->ID, 'project_services', '', ', ', '' ) ); ?> </p></span>
</div><!--projects-on-hover-tester -->
</div><!--project-image-->
</div><!--projects-column -->
<?PHP endif; ?>
<?php
endwhile;
endif;
?>
<?php wp_reset_postdata(); ?>
You just want to change the way you are processing the post details in your loop, so you don't need to make any changes to the loop itself. What you want to do is check the custom field inside the loop and then decide what to do with it.
Also FYI, you are using the_field - this will display the result to the screen immediately. Instead, to use the value without automatically displaying it, use get_field
Take a look at this - there are additional comments to help explain what it happening:
<?php
$the_query = new WP_Query($args);
// Standard while loop to get each of the returned posts one by one -
// we don't care what the post type is here, we just load it into the global "post" so we can use it...
if( $the_query->have_posts(); while ($the_query->have_posts() ) : $the_query-
>the_post();
// Now the details of that post are loaded into the global "post",
// so we can check the values in that post
// Check if the current post in out loop has commercial-damages
if (get_field('project_services') === 'commercial-damages'):
?>
// do whatever you want with the commercial-damages posts...
<?php else: ?>
// do whatever you want with the other posts
<?php endif; ?>
<?php
endwhile;
endif;
?>
Thank you to FluffyKitten who helped me work through this. I wasn't able to get an if loop to work exactly how outlined in the question, but I tried a few other approaches and just got one to work. I created an if / else statement in the HTML that I am looping. There I put if this field is empty, use another which I created and labeled differently to solve my problem. Below is the code:
<?php if( get_field('project_value') ): ?><p><span style="font-weight: bold;">PROJECT VALUE: </span><?php the_field('project_value'); ?></p>
<?php else: ?>
<p><span style="font-weight: bold;">CLAIMS VALUE: </span><?php the_field('claims_value'); ?></p><?php endif; ?>

How can I get post into my homepage?

I want to get post into my wordpress homepage, I tried to search lot from google but I am not geting any proper solutions, So if any one have idea please help me.
Thanks in advance.
Here is my tried code so far :
<?php
$args = array(
'cat' => '5',
'post_type' => 'post',
'posts_per_page' => 8,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content,0,300)." . . . ";
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php
endwhile;
?>
</div>
<div id="page-gina">
<?php
//wp_pagenavi();
wp_reset_query(); // Restore global post data
?>
</div>
Two Ways is possible,
i)Create a custom template.
ii)In your page.php check condition.
Ex:
<?php if(is_front_page()){
echo "This Home";//Here Your code
}
?>

wp_get_archives, one post missing

i'm trying to built a custom archive page.
there is a dropdownmenu for the year, whichs displays the post count (correctly). but when i pick one year, one post (the newest) is missing.
this is my dropdown
<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=""><?php echo esc_attr( __( 'Jahr' ) ); ?></option>
<?php wp_get_archives( array( 'type' => 'yearly', 'format' => 'option', 'show_post_count' => 1 ) ); ?>
and there is my loop
<?php while(have_posts()) : the_post(); ?>
<div <?php post_class('post-programm'); ?>>
<a href="<?php the_permalink(); ?>">
<div class="entry-content">
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; ?>
i also tried a query_posts, but in this case i get all the posts, not only for one particular year.
<?php query_posts('posts_per_page=-1');
if(have_posts()) : while(have_posts()) : the_post(); ?>
thanks in advance for any help!
Is your one missing post a custom post type? wp_get_archives doesn't work with custom posts.

WP if condition showposts

I have this code which works okay for what I wanted but I will probably need to put a condition if there's only 2 posts then wrap it in a <div class="large-6"> then if there's 3 posts then wrap it in large-4.
Just a little confused how to add a condition statement.
<?php
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'showposts' => '3', 'offset' => '1' ) );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="large-4 columns">
<h5><?php the_title(); ?></h5>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php echo get_the_post_thumbnail(); ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
To Pieter:
something like this? if so, that didn't work though unless I'm doing it totally wrong and noobishly.
<?php
$query = new WP_Query(array(
'posts_per_page' => '3',
'post_type' => 'portfolio',
'offset' => '1'
));
while ($query->have_posts()): $query->the_post(); ?>
<?php if(!isset ($query->posts[2])){ ?>
<div class="large-6 columns">
<h5><?php the_title(); ?></h5>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php echo get_the_post_thumbnail(); ?>
<?php endif; ?>
</div>
<?php } else {?>
<div class="large-4 columns"> </div>
<?php }?>
<?php endwhile; ?>
This is just a theory but should work. Here is the idea:
$loop->posts holds an array with all the posts with all their resepective postdata. So, with that in mind, it should be easy to determine whether you have one, two or three posts in that array by simply checking if a certain array key exists
So you basically needs to check if you have a third post, so you can try something like this
if( !isset( $loop->posts[2] ) ) {
//add div if less than 3 posts
}else{
//add div if you have 3 posts
}
EDIT
Just a note. The OP has changed the query variable from $loop to $query in his original code. $loop should be changed accordingly
You can put conditionals like
<?php if(condition) : ?>
code to execute if condition == true
<?php elseif(condition2) :?> // repeat as many times as necessary
...
<?php else :?>
final else if needed, you can just end the if statement if it's only one condition, if you have else if else this is needed
<?php endif; ?>
That's basically it...

Strage Problem in Wordpress Custom Post Type?

I have a blog, and I have added one custom post type 'Movies'. and I have link it to my single-movies.php. and added this code:
<div id="container">
<?php
$args = array( 'post_type' => 'movies', 'posts_per_page' => 1, 'name' => $_GET['movie'] );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1>
<a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
</h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php include(TEMPLATEPATH . '/sidebar_single.php'); ?>
<?php get_footer(); ?>
But Problem is, I'm clicking on any movie, its showing me only same movie, which i have added last on custom field Movies (i.e Avatar Movie, if I click on stargate movie, Its showing me 'Avatar' Movie link. and same others)
Please help me and change this code, if I did anything wrong here.
when you are in single-movies.php
it means you are already on the right path like /movies/avatar
so doing a simple loop
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); //.... ?>
<?php endwhile; ?>
gives you all details of the avatar movie, you don't need to query, because the query is already made by the time you arrive to a SINGLE Movie page!
/movies/avatar means:
post type = movies
post name = avatar
also if you have pretty links, $_GET['movie'] it normally contains nothing.
it is prefered to use the $wp->query_vars['post_name'] to get the movie name
try just putting the loop and see what you get, if not dump the $_GET and $wp to see where you have the movie name.
you have a problem with the loop.
I would say to use a foreach...look at this example, maybe it will help you. it is exactly the same as you want to achieve.
<?php
$cat_id = $category->cat_ID; // YOU CAN CHANGE THIS OR REMOVE
global $post;
$args = array( 'category' => $cat_id, 'numberposts' => -1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : start_wp();
?>
<li class="subcat-post-title">
<span class="trigger">
<a class="ficha" href="<?php the_permalink() ?>">
<?php echo the_title(); ?>
</a>
<?php endforeach; ?>

Resources