-1

I have managed to integrate a custom taxonomies plugin that:

-Filters down the staff depending on what category is selected.

I want it to show that department first, and then the other departments beneath rather than that department all on its own (how it is now).

Here is the link: http://crippslawtest.co.uk/people/

I am guessing I need to add a second part to my loop, that says "And display all posts from all other categories afterwards".

Here is my Wordpress loop:

        <div class="staffwrapper">
    <?php
    $args = array( 'post_type' => 'cripps_staff', 'posts_per_page' => 300 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();

    echo '<div class="col-md-3 spacetop">';
    echo '<a href="'.get_permalink().'">';
    echo '<img src="';
    echo get_post_meta($post->ID,'image',true);
    echo '">';
    echo '</a>';
    echo '<h2 class="staffname">';
    echo get_post_meta($post->ID,'staff_name',true);
    echo '</h2>';
    echo '<h2 class="staffrole">';
    echo get_post_meta($post->ID,'staff_role',true);
    echo '</h2>';
    echo '<h2 class="staffnumber">';
    echo get_post_meta($post->ID,'staff_telephone_number',true);
    echo '</h2>';
    echo '<h2 class="staffemail">';
    echo get_post_meta($post->ID,'staff_email_address',true);
    echo '</h2>';
    echo '</div>';
    endwhile;
    ?>
    </div><!--End of staff wrapper-->

I have tried to make a 2nd query within my loop with the help of this page: http://codex.wordpress.org/Class_Reference/WP_Query#Methods_and_Properties

What I can't figure out is how to write the argument that pulls "All other posts", any ideas?

Here is my attempt:

<?php
    $args = array( 'post_type' => 'cripps_staff', 'posts_per_page' => 300 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();

    echo '<div class="col-md-3 spacetop">';
    echo '<a href="'.get_permalink().'">';
    echo '<img src="';
    echo get_post_meta($post->ID,'image',true);
    echo '">';
    echo '</a>';
    echo '<h2 class="staffname">';
    echo get_post_meta($post->ID,'staff_name',true);
    echo '</h2>';
    echo '<h2 class="staffrole">';
    echo get_post_meta($post->ID,'staff_role',true);
    echo '</h2>';
    echo '<h2 class="staffnumber">';
    echo get_post_meta($post->ID,'staff_telephone_number',true);
    echo '</h2>';
    echo '<h2 class="staffemail">';
    echo get_post_meta($post->ID,'staff_email_address',true);
    echo '</h2>';
    echo '</div>';
    endwhile;

    // 2nd loop
    wp_reset_postdata();
    /* The 2nd Query (without global var) */
    $query2 = new WP_Query( $args2 );
    // The 2nd Loop
    while( $query2->have_posts() ) {
    $query2->next_post();
    echo '<div class="col-md-3 spacetop">';
    echo '<a href="'.get_permalink().'">';
    echo '<img src="';
    echo get_post_meta($post->ID,'image',true);
    echo '">';
    echo '</a>';
    echo '<h2 class="staffname">';
    echo get_post_meta($post->ID,'staff_name',true);
    echo '</h2>';
    echo '<h2 class="staffrole">';
    echo get_post_meta($post->ID,'staff_role',true);
    echo '</h2>';
    echo '<h2 class="staffnumber">';
    echo get_post_meta($post->ID,'staff_telephone_number',true);
    echo '</h2>';
    echo '<h2 class="staffemail">';
    echo get_post_meta($post->ID,'staff_email_address',true);
    echo '</h2>';
    echo '</div>';
    // Restore original Post Data
    wp_reset_postdata();
    }
    ?>

Thank you!

  • I don't fully understand what you are trying to do. Can you explain better the structure of your custom post type and custom taxonomies and what you do you want to output? I don't see any argument that filter by department. Also, I don't see the value of $args2. – cybmeta May 1 '14 at 10:51
  • Additionally: you can make your code more readable by not using so many echos ;-) – birgire May 1 '14 at 15:41
0

Your code makes it hard for anyone to help you. It is important to properly edit your code in your question. You really don't have to use the amount of echo's you are using. It is just poor formatting and poor use of proper tags. You can clean up your code by just properly opening and closing php tags. Here is how your code should look like. BTW, your link is useless to us as your site is only viewable to registered users.

First set of code

?>
  <div class="staffwrapper">
    <?php
        $args = array( 'post_type' => 'cripps_staff', 'posts_per_page' => 300 );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
    ?>

    <div class="col-md-3 spacetop">
        <a href="<?php get_permalink(); ?>"><img src="<?php echo get_post_meta($post->ID,'image',true); ?>"></a>

        <h2 class="staffname">
            <?php echo get_post_meta($post->ID,'staff_name',true); ?>
        </h2>

        <h2 class="staffrole">
            <?php echo get_post_meta($post->ID,'staff_role',true); ?>
        </h2>

        <h2 class="staffnumber">
            <?php echo get_post_meta($post->ID,'staff_telephone_number',true); ?>
        </h2>

        <h2 class="staffemail">
            <?php echo get_post_meta($post->ID,'staff_email_address',true); ?>
        </h2>

    </div>
    <?php endwhile; ?>
    </div><!--End of staff wrapper-->
<?php

Second set of code

<?php
 $args = array( 'post_type' => 'cripps_staff', 'posts_per_page' => 300 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
?>  

    <div class="col-md-3 spacetop">
        <a href="<?php get_permalink(); ?>"><img src="<?php echo get_post_meta($post->ID,'image',true); ?>"></a>

        <h2 class="staffname">
            <?php echo get_post_meta($post->ID,'staff_name',true); ?>
        </h2>

        <h2 class="staffrole">
            <?php echo get_post_meta($post->ID,'staff_role',true); ?>
        </h2>

        <h2 class="staffnumber">
            <?php echo get_post_meta($post->ID,'staff_telephone_number',true); ?>
        </h2>

        <h2 class="staffemail">
            <?php echo get_post_meta($post->ID,'staff_email_address',true); ?>
        </h2>

    </div>
    <?php endwhile;

        // 2nd loop
        wp_reset_postdata();
        /* The 2nd Query (without global var) */
        $query2 = new WP_Query( $args2 );
        // The 2nd Loop
        while( $query2->have_posts() ) {
        $query2->next_post();

    ?>

    <div class="col-md-3 spacetop">

        <a href="<?php get_permalink();?>"><img src="<?php echo get_post_meta($post->ID,'image',true); ?>"></a>
        <h2 class="staffname">
            <?php echo get_post_meta($post->ID,'staff_name',true); ?>
        </h2>

        <h2 class="staffrole">
            <?php echo get_post_meta($post->ID,'staff_role',true); ?>
        </h2>

        <h2 class="staffnumber">
            <?php echo get_post_meta($post->ID,'staff_telephone_number',true); ?>
        </h2>

        <h2 class="staffemail">
            <?php echo get_post_meta($post->ID,'staff_email_address',true); ?>
        </h2>

    </div>
    <?php
    // Restore original Post Data
    wp_reset_postdata();
    }
    ?>

Now, to come to your real problem, a couple of things are missing/wrong here

First of all, as @cybnet said, $args2 in not defined anywhere. I also don't see the need here for starting a new query if "All other posts" are just normal post type posts (this is what I can assume from your code as $args2 are empty). You can simply just start a normal loop. This loop will only loop through normal posts, not CPT's.

Secondly, what is $query2->next_post();. You couldn't have taken this from the codex page you've mentioned. This should be $query2->the_post(); as it has always been.

Thirdly, you said you need to display "All other posts", but you are again getting the same data as your first loop in your second loop, so this I don't understand and for this reason I'm not going to comment further on this.

According to what I see and they way I understand your code and question, here is what I would do

<?php
 $args = array( 'post_type' => 'cripps_staff', 'posts_per_page' => 300 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
?>  

    <div class="col-md-3 spacetop">
        <a href="<?php get_permalink(); ?>"><img src="<?php echo get_post_meta($post->ID,'image',true); ?>"></a>

        <h2 class="staffname">
            <?php echo get_post_meta($post->ID,'staff_name',true); ?>
        </h2>

        <h2 class="staffrole">
            <?php echo get_post_meta($post->ID,'staff_role',true); ?>
        </h2>

        <h2 class="staffnumber">
            <?php echo get_post_meta($post->ID,'staff_telephone_number',true); ?>
        </h2>

        <h2 class="staffemail">
            <?php echo get_post_meta($post->ID,'staff_email_address',true); ?>
        </h2>

    </div>
    <?php endwhile;

        // 2nd loop
        wp_reset_postdata();

        while ( have_posts() ) : the_post();

    ?>

    <--- Your contents of "All other posts" --->

    <?php

You should have a look at this tutorial from catswhocode about multiple loops. You can also check this page in the codex about multiple loops. PLEASE PLEASE PLEASE, this page seriously needs editing as it is using query_posts, which should never be used. WP_Query should be used in all examples.

From what I've given you and these links you should be able to solve your issue

  • Thanks for the awesome response, I have resolved the issue completely, I can't upvote you yet or I definitely would have! – Archie Butler May 2 '14 at 11:29
  • Not a problem. Glad I could help though – Pieter Goosen May 2 '14 at 11:52
0

After battling my way through the darkest realms of Wordpress integrated looping php I have finally managed to get the WP Query to work exactly as the client wanted.

My code was made more difficult than the simple one above because the custom theme I built (based on twenty fourteen theme but integrated with css bootstrap) was using 'get_template_part', so the loops where all happening in different places.

Anyhow, I got it to work after a couple of hours:

<?php if ( have_posts() ) : ?>
            <?php
                // Start the Loop.
                while ( have_posts() ) : the_post();
                $do_not_duplicate[] = $post->ID;
                get_template_part( 'content', 'peopleformat' ); 
                                    // This is where my content display template is
                ?>
                                    <?php endwhile; ?>
                <?php
                // 2nd loop
                    $args = array( 'post_type' => 'cripps_staff','posts_per_page' => 300 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
            if (in_array($post->ID, $do_not_duplicate)) continue;

                ?>

                    <!--Second loop content here-->


                <?php endwhile; ?> 

Then the permalinks were going back to the search results rather than the post permalink, which I resolved with using:

<a href='<?php echo get_permalink($post->ID); ?>'>

Also, as you can see, I had to make use of an array to stop duplication, that starts in the first loop and ends in the second, based on this article: https://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

A very big thanks to Pieter Goosen for pointing me in the right direction, as Kenny would say: "I learnt something today!".

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.