• Resolved cwmuller

    (@cwmuller)


    I’m trying list child pages and some custom field values, and have them print sorted by one of those custom field values.

    At the moment I have this:

    <?php
    	$children = get_pages('child_of=' . $post->ID);
    
        if (!empty($children)) {
    	foreach($children as $child) { 
    
    		$tittel = get_the_title($child->ID);
    		$dato = get_post_meta($child->ID, 'dato', true);
    		$regiss?r= get_post_meta($child->ID, 'regiss?r', true);
    		$nasjonalitet= get_post_meta($child->ID, 'nasjonalitet', true);
    		$produksjons?r= get_post_meta($child->ID, 'produksjons?r', true);
    		$klokkeslett= get_post_meta($child->ID, 'klokkeslett', true);
    		$url= get_permalink($child->ID);
    
    		echo '<p><strong>' . strtoupper(date("l, j. F", strtotime($dato))) . ', KL. ' . $klokkeslett . '</strong><br>';
    		echo '<a href="' . $url . '">' . strtoupper($tittel) . '</a>'; if (!empty($regiss?r)) { echo ' – ' . $regiss?r . ', ' . $nasjonalitet . ', ' . $produksjons?r . '</p>';}
            }
        }
    ?>

    At the moment, the list is just like I want it, except that the pages are not sorted by the custom field ‘dato’, which is what I’d like to do.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    get_pages() can only sort by columns in the posts table. It does use WP_Query, so you may be able to modify the actual query with the ‘posts_orderby’ filter. You’d have to determine how to properly reference the column that contains custom field values in the myuSQL query this function generates.

    Thread Starter cwmuller

    (@cwmuller)

    I’ve been experimenting with that, but since I am very inexperienced with this stuff it’s all about trial and error. I think I ‘m on to something now though. I ran this with and without specifying ‘ascending’ sort order, and I get entries sorted from the beginning and end of the list, but I only get 5 entries, which I don’t get at all. Any suggestions?

    <?php 
    
    	$args = array(
    		'child_of=' . $post->ID ,
    		'meta_key' => 'dato',
    		'orderby' => 'meta_value',
    		'post_type' => 'page',
    		'order' => 'ASC',
    	);
    
    	$children = get_posts($args);
    
        if (!empty($children)) {
    	foreach($children as $child) { 
    
    		$tittel = get_the_title($child->ID);
    		$dato = get_post_meta($child->ID, 'dato', true);
    		$regiss?r= get_post_meta($child->ID, 'regiss?r', true);
    		$nasjonalitet= get_post_meta($child->ID, 'nasjonalitet', true);
    		$produksjons?r= get_post_meta($child->ID, 'produksjons?r', true);
    		$klokkeslett= get_post_meta($child->ID, 'klokkeslett', true);
    		$url= get_permalink($child->ID);
    
    		echo '<p><strong>' . strtoupper(date("l, j. F", strtotime($dato))) . ', KL. ' . $klokkeslett . '</strong><br>';
    		echo '<a href="' . $url . '">' . strtoupper($tittel) . '</a>'; if (!empty($regiss?r)) { echo ' – ' . $regiss?r . ', ' . $nasjonalitet . ', ' . $produksjons?r . '</p>';}
            }
        }
    ?>
    Thread Starter cwmuller

    (@cwmuller)

    Thread Starter cwmuller

    (@cwmuller)

    Never mind, I figured it out. The default number of posts turned out to be five, so it was just a matter of adding.

    'numberposts'

    And using get_posts instead of get_pages was able to call a list of sub-pages for you?

    Moderator bcworkz

    (@bcworkz)

    Good call using get_posts(), which does accept an orderby meta_value argument!

    @jhoffm34: As long as 'post_type' => 'page' is part of the arguments, you will get pages. Pages, after all, are just a particular type of post.

    Awesome ?? Good to know.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Sort get_pages by custom field’ is closed to new replies.