• Resolved jonizen

    (@jonizen)


    Hey, I can’t find a way to get pages by meta data.

    Basically I have a page with meta data ‘commercials’ and i want all posts with the same meta data to show. I need this to be dynamic though and cannot figure it out. Any tips?

    $postslist = get_posts(‘numberposts=8; ?>’);

Viewing 4 replies - 1 through 4 (of 4 total)
  • The template tag, query_posts(), shows most of the arguments available to get_posts.

    If by ‘meta data’ you are referring to custom fields, this should:

    $postslist = get_posts('meta_value=commercials&numberposts=8');

    [edit fixed example]

    Thread Starter jonizen

    (@jonizen)

    thanks for the help michael, but the problem with this is i need it to be dynamic. it needs to find out what the custom field is for this page and then find all the related posts.

    meanwhile it wouldn’t let me put any php jargon in the quotation as far as i could tell.

    You want to use Function_Reference/get_post_custom_keys then use the value you retrieve for your get_posts. So this example would use the last custom key found on a post to get 8 other posts with that custom field.

    <?php
      $custom_field_keys = get_post_custom_keys();
      foreach ( $custom_field_keys as $key => $value ) {
        $valuet = trim($value);
          if ( '_' == $valuet{0} )
          continue;
        $post_custom_key = $value;
      }
      $postslist = get_posts('meta_value='.$post_custom_key.'&numberposts=8');
    ?>

    Thread Starter jonizen

    (@jonizen)

    thank you, works great!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘get pages by meta data’ is closed to new replies.