WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sort get_pages by custom field (8 posts)

  1. cwmuller
    Member
    Posted 1 year ago #

    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.

  2. bcworkz
    Member
    Posted 1 year ago #

    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.

  3. cwmuller
    Member
    Posted 1 year ago #

    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>';}
            }
        }
    ?>
  4. cwmuller
    Member
    Posted 1 year ago #

  5. cwmuller
    Member
    Posted 1 year ago #

    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'

  6. Jay Hoffmann
    Member
    Posted 1 year ago #

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

  7. bcworkz
    Member
    Posted 1 year ago #

    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.

  8. Jay Hoffmann
    Member
    Posted 1 year ago #

    Awesome :) Good to know.

Topic Closed

This topic has been closed to new replies.

About this Topic