Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need two post queries to display on one page WordPress

  • SOLVED

Hello,

I'm really puzzled on a query - I've got 2 loops running on a page, I may be doing this completely arse about face, so if any one can advise, would be hugely helpful. :)

See mark up below (is using jquery mobile mark up)



<?php if (is_page('exhibitors-stands')) { ?>

<h3>Browse Manufacturers</h3>

<ul data-role="listview" data-theme="<?php jqtheme_mainlist(); ?>" data-inset="true">

<?php query_posts('&category_name=manufacturer&showposts=-1&orderby=title&order=asc'); ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<li>

<a href="<?php the_permalink() ?>">

<?php the_post_thumbnail('thumbnail', array('class' => 'ui-li-icon')); ?>

<?php the_title(); ?>

</a>

</li>

<?php endwhile; endif; ?>

</ul>


<h3>Exhibitors A-Z</h3>

<ul data-role="listview" data-theme="<?php jqtheme_mainlist(); ?>" data-inset="true">

<?php

if (is_category('exhibitors'))
{
$posts = query_posts($query_string .
'&posts_per_page=-1&orderby=title&order=asc&cat=-16');
}
$last_initial = "";
if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php

$current_initial = get_the_title();
$current_initial = strtoupper($current_initial[0]);
if($current_initial != $last_initial) {
$last_initial = $current_initial;

?>

<li data-role="list-divider"><?php echo $current_initial; ?></li>

<?php } ?>

<li>

<a href="<?php the_permalink() ?>">

<?php the_title(); ?>

</a>

</li>

<?php endwhile; endif; ?>

</ul>

<?php } ?>




The results of the loops above are...

<strong>Browse Manufacturers</strong>

This 1st query is a list which lists all my posts in category 'manufacturers' - this working correctly

<strong>Exhibitors A-Z</strong>

This 2nd query should be listing all my posts in category 'exhibitors' with alphabetized list dividers, but excluding the posts in category 'manufacturers' <em>(exhibitors is a parent category of manufacturers)</em>. Currently the results of this loop is only listing the 'manufacturers' category with alphabetized list dividers, which is wrong and I cant figure it out.


I hope this makes sense, if any one needs me to elaborate more don't hesitate to post, money will go to the first person who post's working code. Thanks in advance.

Answers (3)

2011-10-20

Luis Abarca answers:

Use WP_Query instad of query_posts


$first = new WP_Query(...);

if ( $first->have_posts() ) {
While ( $first->have_posts() ) {
$first->the_post();
the_title();
}
}


Josh Cranwell comments:

same results...



if (is_page('exhibitors-stands'))
{
$posts = WP_query($query_string .
'&posts_per_page=-1&orderby=title&order=asc&cat=-16');
}
$last_initial = "";
if (have_posts()) : while (have_posts()) : the_post(); ?>



Still getting exactly the same list as the first loop but with the added dividers.

:/


Luis Abarca comments:

Updated !


<?php if (is_page('exhibitors-stands')) { ?>
<h3>Browse Manufacturers</h3>
<ul data-role="listview" data-theme="<?php jqtheme_mainlist(); ?>" data-inset="true">
<?php $first = new WP_Query('category_name=manufacturer&showposts=-1&orderby=title&order=asc'); ?>
<?php if ( $first->have_posts()) : while ($first->have_posts()) : $first->the_post(); ?>
<li>
<a href="<?php the_permalink() ?>">
<?php the_post_thumbnail('thumbnail', array('class' => 'ui-li-icon')); ?>
<?php the_title(); ?>
</a>
</li>
<?php endwhile; unset($first); endif; ?>
</ul>

<h3>Exhibitors A-Z</h3>
<ul data-role="listview" data-theme="<?php jqtheme_mainlist(); ?>" data-inset="true">
<?php
if (is_category('exhibitors')) {
$second_posts = new WP_Query($query_string .
'&posts_per_page=-1&orderby=title&order=asc&cat=-16');
}
$last_initial = "";
if ($second_posts->have_posts()) : while ($second_posts->have_posts()) : $second_posts->the_post(); ?>
<?php
$current_initial = get_the_title();
$current_initial = strtoupper($current_initial[0]);
if($current_initial != $last_initial) {
$last_initial = $current_initial;
?>
<li data-role="list-divider"><?php echo $current_initial; ?></li>
<?php } ?>
<li>
<a href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; unset($second_posts); endif; ?>
</ul>
<?php } ?>


Josh Cranwell comments:

Absolutly perfect, can see what you did there - also I was missing the unset when I tried.

Many thanks Luis

2011-10-20

Luis Cordova answers:

I think you should be using the rewind function and also it is obvious that the query_posts is not the method to use in this occasion, rather use wp_query

try that

if that does not work then it is because you should not use

if (have_posts()) : while (have_posts()) : the_post(); ?>

as loop but loop differently on a more custom way, check the codex it talks about multi loops

hope that helps


Luis Cordova comments:

also and most likely

are you sure you are entering the condition here:

if (is_category('exhibitors'))

{


?
that should be it


Josh Cranwell comments:

Thanks for your response, Now I've removed this.. second loop top section looks like this..



$posts = query_posts($query_string .
'&cat=4&posts_per_page=-1&orderby=title&order=asc&cat=-16');

$last_initial = "";
if (have_posts()) : while (have_posts()) : the_post(); ?>



Category id 4 is category 'exhibitors'

Category id 16 is category 'manufacturers'

For some reason list now only displays my page name which is Exhibitors & Stands inside the list with the list divider above with 'E' in it. The link doesn't do anything.

:-/


Luis Cordova comments:

yes switch to WP_Query as i had told you also


$first = new WP_Query(...);


if ( $first->have_posts() ) {

While ( $first->have_posts() ) {

$first->the_post();

the_title();

}

}

$second = new WP_Query(...);



if ( $second->have_posts() ) {

While ( $first->have_posts() ) {

$second->the_post();

the_title();

}

}


the right way to do loops


Luis Cordova comments:

do not use just have_posts() but rather

what i told you above

$first->

$second->


Josh Cranwell comments:

Thanks for your help but I can't really get my head around this, i've adding in the

$first->

$second->

into my loops but something is going wrong, I'll back to you if I figure it out but I can't seem to work it out so far.


Luis Cordova comments:

it works, message me on skype cordova_luis and we can look at the code together


Luis Cordova comments:

I think you will figure out, the other poster also post some explicit code, it is what we have been telling you. Hope it helps.


Josh Cranwell comments:

Dude appreciate your time for the response but had to give it to the other Luis as he posted fully working code. Can see what you were angling at me to do, but I was missing the unset an the end of the query. Thanks for your time dude.

2011-10-21

Gerard answers:

Try to use this:

<?php wp_reset_query(); ?>

Just before your second query_posts() call:

$posts = query_posts($query_string . '&cat=4&posts_per_page=-1&orderby=title&order=asc&cat=-16');

And see if it makes any difference


Josh Cranwell comments:

Luis code above your post work, but thanks dude