wordpress custom field listing to alphabet

How can we list custom fields alphabetically in this WordPress code?

<?php while ( have_posts() ) : the_post(); ?>
<?php
global $wp_query;
$postid = $wp_query->post->ID;
wp_reset_query();
?>

        <h2 class="post-box-title">
            <a href="<?php the_permalink(); ?>"><?php echo get_post_meta($postid, 'ruya', true); ?></a>
        </h2>

<?php endwhile;?>

2 answers

  • answered 2018-01-13 17:46 Will Hines

    <?php
        $custom_fields = get_post_custom(); // gets custom fields for current post
        ksort($custom_fields); // sorts by key
        foreach ($custom_fields as $key => $values) {
            echo "<p><b>$key: </b> ";
            foreach ($values as $v) { // could be more than one value
                echo "$v ";
            }
            echo "<p>\n";
        }
    

    If for some reason get_post_custom() doesn't know what post to retrieve try this:

      $custom_fields = get_post_custom($postid);
    

  • answered 2018-01-13 17:46 Güven Zafer Coşkun

    thank you I solved the problem myself

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
    'meta_key' => 'ruya',
    'orderby' => 'meta_value',
    'paged' => $paged,
    'order' => 'ASC'
    );
    $wp_query->query($args);
    if(have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <h2 class="post-box-title">
                <a href="<?php the_permalink(); ?>"><?php echo get_post_meta($post->ID, "ruya", true); ?></a>
            </h2>
    <?php endwhile; endif; ?>