Forums

Display data from custom fields (8 posts)

  1. baga
    Member
    Posted 2 years ago #

    Hello I have a few pages which use custom fields, e.g. 'email' and 'url'; I was wondering how to display on a new page a list of urls retrieved from all pages which use the 'url' custom field.

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    List all pages and then all posts with specific custom field

    <?php
    $type = 'page';
    $args=array(
        'post_type' => $type,
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'meta_key' => 'url',
        'caller_get_posts'=> 1
        );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'List of: ' . $type .'(s)';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
         <?php
        endwhile;
      } //if ($my_query)
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Related:
    query_posts()
    Custom Fields
    Page Templates

  3. baga
    Member
    Posted 2 years ago #

    Thanks MichaelH!

    I added the line:
    <p><?php echo get_post_meta($post->ID, 'url', true); ?></p>
    and it works perfectly.

    What should I add if I wanted to display only urls or other cutom fields from subpages of the current page?

  4. MichaelH
    Volunteer
    Posted 2 years ago #

    What should I add if I wanted to display only urls or other cutom fields from subpages of the current page?

    Could use the 'post_parent' => $post[0]->ID, argument

    $args=array(
        'post_type' => $type,
        'post_parent' => $post[0]->ID,
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'meta_key' => 'url',
        'caller_get_posts'=> 1
        );
  5. baga
    Member
    Posted 2 years ago #

    Thanks a lot MichaelH ;-)

  6. baga
    Member
    Posted 2 years ago #

    Could use the 'post_parent' => $post[0]->ID, argument

    When i use this argument, it doesn't display anything, am I missing something?

  7. baga
    Member
    Posted 2 years ago #

    What if I wanted to display only urls or other cutom fields from the parent page of the current page?

  8. baga
    Member
    Posted 2 years ago #

    Any suggestion regarding my last question?

Topic Closed

This topic has been closed to new replies.

About this Topic