Support » Fixing WordPress » display custom fields from all posts in a specific category?

  • Ok, I have been searching for a while and I cannot find a graceful, working solutions. What I want to do is to have a landing page using a specific page template. On that page I would like a section that displays a list of custom field values from each post in a set category. Would look something like…

    [custom field 1] [custom field 2]
    [custom field 1] [custom field 2]

    Each row is from a different post in that category.

    Thoughts?

    Thanks for the help in advance!

Viewing 15 replies - 1 through 15 (of 33 total)
  • I’m not experienced with custom feilds, but from what I know, wouldn’t you just make “the_meta” as the output for posts in that category?

    Moderator justingreerbbi

    (@justingreerbbi)

    Forum Moderator

    Read up on the The Loop..

    You have to run a loop of the post and then just grab meta of each post with something like get_post_meta().

    Sorry I don’t have time for an example snippet but you should be able find it by searching for “The Loop” and “post meta”

    Just adjust posts per page and the category id;)

    <?php query_posts( array(      'posts_per_page' => -1,      'cat' => '1',     'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), ));
    
    if (have_posts()) {
       while (have_posts()) {
          the_post();
    get_post_meta();?>

    If you want post rows:

    <?php
    // Show a selected number of posts per row
    $posts_per_row = 2;
    $posts_per_page = -1;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
       'posts_per_page' => $posts_per_page,
       'paged' => $paged,
    
    );?>
    <?php query_posts( array(      'posts_per_page' => -1,      'cat' => '1',     'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), ));
    
    if (have_posts()) {
       while (have_posts()) {
          the_post();
    
    if ((++$post_counter % $posts_per_row) == 1  || $posts_per_row == 1) {
             if ($post_counter > 1) {
                echo "</div><!-- End of post_row -->\n";  // End previous row
             }
             echo "<div class='post_row'>\n";  // Start a new row
          }
          echo "<div class='post_class'>\n";  // Start one post
    
    // Output post data here
    
    <?php get_post_meta();?>
    
    <?php
          echo "</div><!-- End of post_class -->\n";  // End of post
       } ?>
       </div><!-- End of post_row -->
       <div class='clear'></div>
    
    <?php } else {
       // Code for no posts found
    }
    ?>

    in case my code gets dumped: pastebin

    I’ll give it a try and see if I can get it to output what I need it to.

    I know the basics of how it should be done in theory…I can see it in my head…its just figure out the actual code that will do it well.

    Thanks!

    That didn’t work… :/

    here’s a bit more of the specifics of what I’m wanting to do…

    there are a group of posts that are in a specific category. each post has a set of custom field values. now i have a page template setup so that on a page it will display the content of those posts in that set category. i am wanting to go one step further by having a small list in the page’s sidebar that displays that set of custom fields from those posts. I want to take each custom field and put them together in a row. (field1 + field2 + field3), etc. Each set of custom field values will be displayed in a single row…each row of values is pulled from each post that is in that set category.

    Additionally I am trying to get the category value to be modular. by that I mean so that I won’t have to set a specific value in there to call the category. It would pull that value from what is being displayed on the page.

    I know its a bit complex, but I hope it makes sense.

    Thanks!

    Moderator justingreerbbi

    (@justingreerbbi)

    Forum Moderator

    You can modify the first example snippet to achieve this. Those example are not going to work out of the box since they are basic examples form the codex.

    <?php
    global $post;
    query_posts( array('post_type'=> 'posts'));
    
    if (have_posts()) {
       while (have_posts()) {
      $custom_field_keys = get_post_custom_keys($post->ID);
      foreach ( $custom_field_keys as $key => $value ) {
        $valuet = trim($value);
          if ( '_' == $valuet{0} )
          continue;
        echo $key . " => " . $value . "<br />";
      }
    ?>

    Not tested nut that will print out and the meta keys and the value of what ever query is looking for

    yikes.. maybe once you get a start on it we can get a better idea of where it’s going and help with the details. I’d hate to dump a bunch of time in only to find it’s not what you want. Good luck, sounds like an iteresting goal. Look forward to helping.

    Well I know how to pull custom field values. Just not to narrow the results down to being pulled from all posts in a set category. and then where the category value isn’t set as a hard value, where its modular based on what is called on the page, etc.

    I’ll let everyone know once I either come close or have found a complete solution.

    Moderator justingreerbbi

    (@justingreerbbi)

    Forum Moderator

    Come on man.. Read the Codex

    If you are looking at a page that has a category then check for the category against the page post ID

    $category = get_the_category($post->ID);
    $categoryname = $category[0]->cat_name;

    then

    global $post;
    query_posts( array('post_type'=> 'posts', 'category_name' => ''.$categoryname.''));
    
    if (have_posts()) {
       while (have_posts()) {
      $custom_field_keys = get_post_custom_keys($post->ID);
      foreach ( $custom_field_keys as $key => $value ) {
        $valuet = trim($value);
          if ( '_' == $valuet{0} )
          continue;
        echo $key . " => " . $value . "<br />";
      }

    This will pull all the keys and values for the category that that page belongs to..

    First, I’m not a noob when it comes to WordPress. I wouldn’t have come here if the solution would have simply been found in the codex.

    Second, by default, you can’t map categories to pages. To do that I am using a plugin that does this. By default all pages have the default category attached to it. Until WordPress does this by default, the only way to do so is with a plugin. So to get all the post content from posts of a certain category, I am also using another plugin, page posts associator. Its the only way I’ve found to do this without using some overly complex code.

    Sorry, your solution simply isn’t working for me. Your method will work using set values for the category and I could use that if I wanted to create a template page for each one…which I don’t want to do because it would start to add up to a lot of page templates doing the same thing essentially.

    Moderator justingreerbbi

    (@justingreerbbi)

    Forum Moderator

    It seems you may have to tap into what ever plugin you are using to list the posts.

    It will go a bit deeper than just having someone write you a snippet here.

    Moderator justingreerbbi

    (@justingreerbbi)

    Forum Moderator

    Which plugin are you using to list the posts out with

    Post Page Associator.

    I personally don’t like the plugin, especially the dev…the plugin basically spams your admin panel with plugs for the pro version…had to go in and hide all those in the plugin files. If there was a better way to do that, then I would use it, but to this point I haven’t found one. It basically lets you associate posts with pages so they can be displayed using a shortcode…has lots of options and conditionals, etc.

Viewing 15 replies - 1 through 15 (of 33 total)
  • The topic ‘display custom fields from all posts in a specific category?’ is closed to new replies.