0

I have been stressing over this for a while. I have figured out how to call pages in a loop using custom taxonomies. I have also figured out how to call child pages of a parent page in a loop. However, what I would like to do is call the child pages, then filter out the child pages that belong to a custom taxonomy, as to only show these ones.

Thanks in advance for any help,

I can offer any more information needed,

The child page code:

<?php

$child_pages = $wpdb->get_results(
    "SELECT * FROM $wpdb->posts WHERE post_parent = $post->ID AND post_type = 'page' ORDER BY menu_order"
);

if ( $child_pages ) :
    foreach ( $child_pages as $pageChild ) :
        setup_postdata( $pageChild );

        ?>

        <div class="comparison-post-block">
            <div class="wrapper">
                <div class="top-5-image">
                    <?php echo get_the_post_thumbnail( $pageChild->ID, 'thumbnail' ) ?>
                </div>
            </div>
            <div class="comparison-top5-text">
                <div class="wrapper">
                    <div class="top-5-title">
                        <p class="comparisontitle">
                            <a class="comparisontitle" href="<?php echo get_permalink( $pageChild->ID ) ?>" rel="bookmark" title="<?php echo $pageChild->post_title ?>">
                                <?php echo $pageChild->post_title ?>
                             </a>
                        </p>
                    </div>
                </div>
                <div class="wrapper">
                    <div class="comparison-content">
                        <div class="comparisonexcerpttext">
                            <?php echo get_the_excerpt() ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    <?php endforeach ?>
<?php endif ?>

The custom taxonomy code:

<?php 

if ( is_page() ) :
    global $post; 

    $args = array( 
        'tax_query' => array(
            array(
                'taxonomy' => 'top5',
                'field' => 'name',
                'terms' => $post->post_title
            ),
        ),
    );

    $My_Query = new WP_Query( $args );

    if ( $My_Query->have_posts() ) :
        while ( $My_Query->have_posts() ) :
            $My_Query->the_post();
            ?>

            <div class="comparison-stories">
                <div class="comparison-post-block">
                    <div class="wrapper">
                        <div class="top-5-image">
                            <?php the_post_thumbnail( 'medium' ) ?>
                        </div>
                    </div>
                    <div class="comparison-top5-text">
                        <div class="wrapper">
                            <div class="top-5-title">
                                <p class="comparisontitle">
                                    <a class="comparisontitle" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                                        <?php the_title(); ?>
                                    </a>
                                </p>
                            </div>
                        </div>  
                        <div class="wrapper">
                            <div class="comparison-content">
                                <div class="comparisonexcerpttext">
                                    <?php the_excerpt() ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        <?php endwhile ?>

    <?php wp_reset_postdata() ?>
    <?php else : ?>

    <?php endif ?>
<?php endif ?>

As far as I am aware I have copied all of the code into here,

Sorry this is my first post so not exactly sure how you would like the code, so essentially I would like to combine these two pieces of code!

Thanks again!

  • 1
    Could you post the code you have so far? – TheDeadMedic Jun 2 '14 at 16:33
  • Hi, thanks for replying, I have copied the code into my answer above! – user3700046 Jun 3 '14 at 19:57
0

You can grab the ID's of all children and set the post__in argument for your tax query:

$child_ids = $wpdb->get_col(
    "SELECT ID FROM $wpdb->posts WHERE post_parent = $post->ID AND post_type = 'page' ORDER BY menu_order"
);

$args = array(
    'post__in' => $child_ids, // Only retrieve taxonomy posts that are children of this page
    'tax_query' => array(
        array(
            'taxonomy' => 'top5',
            'field' => 'name',
            'terms' => $post->post_title
        ),
    ),
);

Alternatively, if you only want posts that were not children of the page, you can just use the argument 'post_parent__not_in' => array( $post->ID ).

  • Hi, I am not exactly sure where in the loop I should put this. I went ahead and put it in place of the custom taxonomy code, however, now nothing shows on the page and it shows completely white. Any help would be great thanks. – user3700046 Jun 4 '14 at 20:37
  • In what context are you placing your code? Are you sure $wpdb is in scope? Set WP_DEBUG as true to display errors. – TheDeadMedic Jun 5 '14 at 8:01
  • Sorry, for the late reply, I am not sure what $wpdb being 'in scope' means? Thanks – user3700046 Jun 9 '14 at 15:19
  • If you could tell me what to wrap this loop in, so everything but the part of the code that gets the post content that would be incredible and would really save me, if not could you please give me some sort of clue. Thank you very much for all your help! – user3700046 Jun 10 '14 at 21:17

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.