• Resolved deurtje

    (@deurtje)


    Hello,

    I’m working on a custom page template that will show me every grandchild of a certain page that has a certain meta key and meta value. The thing is I have multiple artist pages and I want to show all the albums of that artist on this page, using that certain meta key and meta value. However, my code is failing me.

    My pages are build up as followed:

    – Albums (parent page) [page id = 127]
    — Albums from 2012 (child page) [page id = 6414]
    — Album 1 (grandchild)
    — Album 2 (grandchild)
    — Album 3 (grandchild)
    — etc.
    — Albums from 2011 (child page) [page id = 6423]
    — Album 4 (grandchild)
    — Album 5 (grandchild)
    — etc.

    My code is as followed:

    <?php $mypages = get_pages(array( 'child_of' => 127, 'sort_column' => 'post_date', 'sort_order' => 'desc', 'meta_key' => 'artist', 'meta_value' => $post->post_name )); ?>
    
                    <?php foreach($mypages as $page) { ?>
    
                    	<article class="item switched_images">
                            <a href="<?php echo get_page_link($page->ID) ?>" alt="<?php echo $page->post_title ?>"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail', array('title' => '', 'class' => 'des')); ?></a>
                                <h3><a href="<?php echo get_page_link($page->ID) ?>" alt="<?php echo $page->post_title ?>"><?php echo $page->post_title ?></a></h3>
                        </article><!--.item .switched_images-->
    
                    <?php } ?>

    The key of this peace of code is that the slug of the artist page is the same meta value I give an album. This way every artist page should get their own albums listed.

    When i use ‘child_of’ => 127 in cooperation with the meta_key and meta_value it won’t show me any grandchildren. When I remove the bit where it says meta_key and meta_value, it shows me all of the grandchildren. Are these three conflicting with each other, or is there something I forgot?

    Can somebody help me with this?

Viewing 14 replies - 1 through 14 (of 14 total)
  • Do all of the grandchild pages have this meta key & value?

    I don’t understand this –

    The key of this peace of code is that the slug of the artist page is the same meta value I give an album. This way every artist page should get their own albums listed.

    Do you mean that, taking The Killers as an example, you have 4x Pages –

    • Hot Fuss
    • Sam’s Town
    • Sawdust
    • Day and Age

    – that each of these pages will have a custom field ‘artist’, which is set to ‘The Killers’?

    I maybe totally misunderstanding you, but if that is the case, you structure may be overyly complicated. Why not just have a Post with some custom Taxonomies – ‘Artist’ (non-hierarchical), ‘Year’ (non-hierarchical), ‘Genre’ (hierarchical).

    That way, you could simply display all posts from The Killers by going to https://www.yousite.com/artist/the-killers/. You could also use the custom menus (found under ‘Appearence’ in the admin) to build a menu for the site.

    Thread Starter deurtje

    (@deurtje)

    Thank you both for your replies. Taking your example of The Killers:

    The slug of that page would be ‘the-killers’. I use this exact slug on every album page as a custom field ‘artist’. So an album page ‘Hot Fuss’ would have a meta key ‘artist’ and a meta value ‘the-killers’.

    When I have an album from, let’s say Michael Jackson, I’d put ‘michael-jackson’ as a meta value.

    I’m trying to separate album and artist pages from news items for which I use WordPress Posts.

    And it actually works when I change the ‘child_of’ ID to the ID of a child page instead of the parent page (for instance, ‘child_of’ => 6414). And it also works when leave out the parts with meta_key and meta_value on the get_pages line. So showing children with meta values isn’t the problem, it’s showing grandchildren with meta values.

    I hope I made it a bit more clear to you. I appreciate the help though!

    If these meta values are unique to a set of artists, why bother specifying a parent page?

    Thread Starter deurtje

    (@deurtje)

    Well, because they can be used multiple times. Let’s say Michael Jackson has 5 albums, each album would have the meta value ‘michael-jackson’. The artist page for Michael Jackson should list all 5 of these.

    The albums are categorized in years via their parent pages, i.e. 2011, 2010, 2009, etc.

    So I have a main album page which lists every album from every year. Then I have ‘child’ album pages where only albums from that year are listed. And then I have artist pages which should list every album from every year from that artist.

    because they can be used multiple times.

    That part I understand. The part I am confused about is – why just not base the query on the meta value? Why drag page children into it?

    The albums are categorized in years via their parent pages, i.e. 2011, 2010, 2009, etc.

    Ah! Is this why you need page children?

    Thread Starter deurtje

    (@deurtje)

    Yes, that’s exactly why. But those ‘year’ pages/page children are also the problem, causing the grandchildren to not show up. I hope it’s a much clearer to you now, appreciate your efforts!

    I have to admit that I’ve not really played with custom fields in Pages but I’m just wondering if you mightn’t get better results with something like:

    $args = array(
    	'meta_key' => = 'artist',
    	'meta_value' => $post->post_name,
    	'post_type' => 'page',
    	'post_parent' => 127,
    	'orderby' => 'date',
    	'order' => 'DESC'
    );
    $the_query = new WP_Query( $args );
    while ( $the_query->have_posts() ) : $the_query->the_post();?>
    <article class="item switched_images">
    <a href="<?php the_permalink(); ?>" alt="<?php the_title();?>"><?php the_post_thumbnail($'thumbnail', array('title' => '', 'class' => 'des')); ?></a>
    <h3><a href="<?php the_permalink; ?>"><?php the_title(); ?></a></h3>
    </article><!--.item .switched_images-->
    <?php endwhile;
    // Reset Post Data
    wp_reset_postdata();?>

    WARNING: Not tested.

    Thread Starter deurtje

    (@deurtje)

    Unfortunately, your code does exactly the same as mine, only showing children and not grandchildren (thus in my case showing nothing).

    A workaround could perhaps be to duplicate the code and put in all the child pages their IDs instead of the parent ID, but I’d rather avoid this ofcourse.

    So do you want children & grandchildren?

    Thread Starter deurtje

    (@deurtje)

    No no, only grandchildren actually.

    How about:

    <?php $mypages = get_pages(array( 'child_of' => 127 ));
    foreach( $mypages as $mypage ) {
    	$args = array(
    		'meta_key' => = 'artist',
    		'meta_value' => $post->post_name,
    		'post_type' => 'page',
    		'post_parent' => $mypage->ID,
    		'orderby' => 'date',
    		'order' => 'DESC'
    	);
    	$the_query = new WP_Query( $args );
    	while ( $the_query->have_posts() ) : $the_query->the_post();?>
    	<article class="item switched_images">
    	<a href="<?php the_permalink(); ?>" alt="<?php the_title();?>"><?php the_post_thumbnail($'thumbnail', array('title' => '', 'class' => 'des')); ?></a>
    	<h3><a href="<?php the_permalink; ?>"><?php the_title(); ?></a></h3>
    	</article><!--.item .switched_images-->
    	<?php endwhile;
    	// Reset Post Data
    	wp_reset_postdata();
    }
    ?>

    Thread Starter deurtje

    (@deurtje)

    Like a charm! Thank you so much for your trouble.

    Yay! ??

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Grandchildren aren't shown by get_pages when using meta tags’ is closed to new replies.