0
votes

I have made a shortcode inside my plugin, which is working great . The shortcode needs to take some parameters and create a custom loop with output.

One of the parameters is how many posts to output the loop for ($markers)

$args=array(
                'meta_key'=>'_mykey',
                'post_status'=>'publish',
                'post_type'=>'post',
                'orderby'=>'date',
                'order'=>'DESC',
                'posts_per_page'=>$markers,

);

  $wp_query = new WP_Query();
           $wp_query->query($args);

if ($wp_query->have_posts()) : while (($wp_query->have_posts()) ) : $wp_query->the_post();

// do the loop using get_the_id() and $post->id

endwhile;endif;
wp_reset_query();//END query

On occations I will need to have data from ALL posts ($markers = '-1' ) and sometimes only one ($markers = '1' ) or muliple ($markers = 'x').

All of those work great on single pages / posts - but My problem is that when this function is in a place where I have more than one post (!is_single) and ($ markers = '1' )it will always return the data for the LATEST post , and not for the correct one .. (for example in the default wordpress theme, where it would display10 posts - they will all be the same data )

It is obviously a problem of the $post->ID - but how can I have the correct post ID when doing a custom loop OUTSIDE the wp loop ?

I tried to ovverride the problem by

global $post;
 $thePostIDtmp = $post->ID; //get the ID before starting new query as temp id
 $wp_query = new WP_Query();
 $wp_query->query($args);
// Start Custom Loop

if (!is_single()){
$post_id_t = $thePostIDtmp;}
else {
$post_id_t = $post->ID;}

and then use $post_id_t - but it did not seems to work , Should I not use get_the_id() ? or should I not use query (and use get_posts) ??

Any ideas / solutions / thoughts ??

2

2 Answers

1
votes

I would use query_posts(http://codex.wordpress.org/Function_Reference/query_posts)rather than override the $wp object. You should be able to include as many loops on the page as you want with this. If you have problems with this you can use: http://codex.wordpress.org/Function_Reference/wp_reset_query just before you call it.

I find this: http://blog.cloudfour.com/wordpress-taking-the-hack-out-of-multiple-custom-loops/ takes a bit of the pain away too.

0
votes

There are basically two sorts of querying posts in WordPress: Those that alter the main loop and those that do not. If you want to change the main loop like the one used to display category archive pages then use query_posts. It let's you do exactly that. Delete, change and append parameters of the default query to change the outcome of a typical page. query_posts has some drawbacks though.

Then there are queries that are just used to get stuff out of the database to play around with e.g. displaying the latest post titles in the sidebar or the attachments of the current post.

To do that create a new WP_Query object that will build your custom loop independently of the main loop like so:

// The Query
$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();

Then there is get_posts() which is like the little brother of WP_Query. It has an easier interface in my opinion and returns an array with the results that is easier to work with. It looks like this:

$myposts = get_posts( $args );
foreach($myposts as $post) : setup_postdata($post);
     echo "<li>";
     the_title();
     echo "</li>";
endforeach;

Inside the foreach template tags like get_the_id() will work.