WordPress Loop Destroys Post Reference - wordpress

Doing something like this:
<?php $my_query = new WP_Query('category_name=process&posts_per_page=20');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
Destroys the original reference to the single post, which I would want to list afterward. What is the best way to resolve this? I have tried both of the multiple loop examples on the wordpress doc page, and either I misunderstood them, or they don't work. A friend suggested that the only way to do it was to store the original post ID and then to call that post by ID afterward. If that's the solution, then I could use:
<?php $thePostID = $post->ID; ?>
To get the post id, but how do I query a single post by id?
Thanks in advance.

To query single post by ID you can use get_post( $post_id );, for usage check http://codex.wordpress.org/Function_Reference/get_post

Related

wp_reset_query() or wp_reset_postdata()

I'm getting confused with this so I ask with an example:
<ul class="prod">
<?php $woop = new WP_Query($wol);
while ( $woop->have_posts() ) : $woop->the_post(); global $prod; ?>
<h3><?php the_title(); ?></h3>
<?php woocommerce_template_loop_add_to_cart( $woop->post, $prod );?>
<?php endwhile; wp_reset_postdata();?>
</ul>
In this example should I use wp_reset_query() or wp_reset_postdata()?
What I understand is that I had to use wp_reset_postdata() after every custom WP_Query() and wp_reset_query() after every loop using query_posts().
Is that simple as that?
You should never have to use wp_reset_query(), which is only used to restore $wp_query and global post data to the original main query when using query_posts() (which you should never use).
Instead, you should only be using wp_reset_postdata() when you want to restore the global $post variable of the main query loop after a secondary query loop using new WP_Query(). You have used this correctly in your example.

How do I figure out if a record is a page or a post in wordpress

I am in search.php and I get, for a certain query, a set of results (pages and posts). Now, within the loop I am using to display such results, I need to figure out if the current item is actually a post or a page.
How do I do that? Is there a conditional tag to do it? I couldn't find it :(
Thanks
You can use is_page( $post->ID ) for pages and is_single( $post->ID ) for posts.
You can retrieve and check the post type of a post with get_post_type().
echo 'The post type is: ' . get_post_type( get_the_ID() );
The right answer is the following (sorry guys):
<?php while (have_posts()) : the_post(); ?>
<?php if ($post->post_type === "post"): ?>
it's a post!
<?php endif; ?>
<?php endwhile; ?>

wordpress do not duplicate post

I know how to use few loops without duplicate posts.
But My question is:
Suppose I have two loops, each two loop shows 1 post only, both of them have the same newest post. if I use the code below, the duplicate post will be not shown at the second loop, but it is also stop to continue with the next post. how to solve it. million thx!
code:
<?php $my_query = new WP_Query('cat=1,2&posts_per_page=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
first loop
<?php endwhile; ?>
<?php query_posts('cat=10&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); if (in_array($post->ID, $do_not_duplicate)) continue;?>
second loop
<?php endwhile; endif; ?>
I think you need to clean the example up a little bit:
You have $do_not_duplicate = $post->ID;, assuming your variable is the String post_id, then you check if (in_array($post->ID, $do_not_duplicate)) continue;
At this point $do_not_duplicate is NOT an array().
If you want to store an array(), try this: $do_not_duplicate[] = $post->ID;, then you can perform your current check.

How Can I Echo Out 100% of the_post in Wordpress?

So I query the posts, I get back everything that matches, then do my loop - but I want to do some sort of print_r type thing on each loop so I can see 100% of what data is being fetched. I've got a total brain block here.. Help!
Here is my query, and then my loop:
<?php query_posts('post_type=property'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
Just can't figure out how to echo out 100% of what was fetch through each iteration of the loop?
Thanks for the help everyone!
Does it have to use the loop? If not then I would try:
<? $posts_array = get_posts('post_type=property');
print_r($posts_array); ?>
inside your loop
print_r($post);
I like to add the following to the top of any of my single php pages where I want to see the output...
<?php global $post; var_dump($post);die;?>
and in the loop :-
the_post();
global $post;
print_r($post);

Wordpress grid/list view

Does anyone know what the best way to implement a list/grid togglable view in wordpress would be?
To be more clear: the default view shows the posts in a list, I would like to have the option to show them in a grid.
I have so far created a loop which shows only the thumbnails, and included it in another template.
But I don't know how I would link to that view. Would I best off using an archive view?
Thanks.
One of the easiest solutions is to create a page template containing a grid view and add a link to this page in the list view. Very simple but just what you need.
I had the same issue with pagination. WordPress has built in function posts_nav_link that automatically prints links to previous and next pages if needed. The problem is that this function works only with $wp_query instance of WP_Query (this instance is used by default, for example to get page's content or the latest posts in home). So the solution for you:
<?php
$temp=$wp_query;
$wp_query=null;
$wp_query = new WP_Query('showposts=4');
while(have_posts() ) : the_post(); ?>
<?php the_post_thumbnail( array(160,160) );?>
<h2><?php the_title(); ?></h2>
<?php endwhile;
posts_nav_link();
$wp_query = $temp; ?>
I think that's quite obvious and there is no need of explanation :) should be working
Thanks dude. I kind of got it working for now with this:
<?php $latest = new WP_Query('showposts=4'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
<?php the_post_thumbnail( array(160,160) );?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
I think my mistake was trying to get it to use get_template_part( 'loop', 'grid' ); It would show the page, but no posts. Even if used get_template_part( 'loop', 'index' );
I'll need to figure out the pagination, but I'm putting it down for now to work on something else.
Thanks for your help so far! #Gediminas

Resources