WordPress.org

Ready to get started?Download WordPress

Forums

display custom fields from all posts in a specific category? (34 posts)

  1. Kahil
    Member
    Posted 2 years ago #

    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!

  2. deepbevel
    Member
    Posted 2 years ago #

    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?

  3. justingreerbbi
    Member
    Posted 2 years ago #

    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"

  4. deepbevel
    Member
    Posted 2 years ago #

    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();?>
  5. deepbevel
    Member
    Posted 2 years ago #

    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

  6. Kahil
    Member
    Posted 2 years ago #

    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!

  7. Kahil
    Member
    Posted 2 years ago #

    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!

  8. justingreerbbi
    Member
    Posted 2 years ago #

    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

  9. deepbevel
    Member
    Posted 2 years ago #

    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.

  10. Kahil
    Member
    Posted 2 years ago #

    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.

  11. Kahil
    Member
    Posted 2 years ago #

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

  12. justingreerbbi
    Member
    Posted 2 years ago #

    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..

  13. Kahil
    Member
    Posted 2 years ago #

    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.

  14. justingreerbbi
    Member
    Posted 2 years ago #

    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.

  15. justingreerbbi
    Member
    Posted 2 years ago #

    Which plugin are you using to list the posts out with

  16. Kahil
    Member
    Posted 2 years ago #

    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.

  17. Kahil
    Member
    Posted 2 years ago #

    here's the link where you can get an idea of what i'm doing. its a client panel for my customers. didn't like any of the similar solutions out there so I'm making my own.

    http://www.spiderflystudios.com/cap/spiderfly-studios/

    The section at the top that has the invoices, I want to get rid of that. I want to then use custom fields of the posts that each project is listed on. Use those for the invoice values and put a much smaller list in the sidebar where those custom field values for each project is called.

    Hope that makes sense.

    If anyone has better ideas of how to do all or any of this, please do share. I just want to do something custom...

    thanks

  18. deepbevel
    Member
    Posted 2 years ago #

    could you post you're entire page template?
    pastebin

  19. Kahil
    Member
    Posted 2 years ago #

    Its just a simple page template really... Nothing special about the sidebar right now either. The posts themselves are being called using a shortcode. As of right now, I'm getting most of what I want through the use of some great plugins (with and without some custom modifications). Now I am at the point where I have to start doing some custom coding...

    <?php
    /*
    Template Name: Gallery
    */
    ?>
    
    <?php get_header(); ?>
    <div id="pagename">
            <div class="inside">
    <div style="float:right; padding:0px; margin:-7px -10px 0px 0px; background:none; border:none;"><img src="http://www.spiderflystudios.com/wp-content/uploads/Briefcase128.png" width="100" height="100" style="margin-top:-10px;" /></div>
              <h2><?php
    		  	$pagename = get_post_meta($post->ID, "pagetitle_value", $single = true);
    			if ($pagename != '') { echo $pagename; } else { the_title(); }
    		  ?></h2>
              <p>© Spiderfly Studios 2012 - Asset Portfolios</p>
            </div>
          </div>
        </div>
        <!-- container END here -->
      </header>
      <!-- header END here -->
      <!-- content START here -->
      <div id="content">
        <!-- container START here -->
        <div class="container">
          <!-- main-colum START here -->
          <div id="main-colum2" name="top">
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div>&nbsp;</div>
           	 <?php the_content(); ?>
          	<?php endwhile; endif; ?>
    <div style="clear:both; margin-bottom:45px;">&nbsp;</div>
    <div style="position:absolute; bottom:0px;"><a href="#top" class="big-btn grey">Return To Top</a></div>
          </div>
          <!-- main-colum END here -->
              <?php include ('sidebar-asset-portfolios.php'); ?>
        </div>
        <!-- container END here -->
      </div>
      <!-- content END here -->
    </div>
    <!-- site-wrapper END here -->
    <?php get_footer(); ?>
  20. deepbevel
    Member
    Posted 2 years ago #

    query_posts in a single category with meta as output could be used in this, if intersted in doing it that way. Not sure what happens after that. Seems like what you ultimately want is more complex. But if you want posts from a single category on a page with meta (custom feilds) as output, we can do that. Just say when.

  21. Kahil
    Member
    Posted 2 years ago #

    That sounds like what I am trying for...been trying...just can't seem to nail down the code to do it correctly...which is why I came here. My brain is just so tired I'm probably just not seeing the solution right in front of me. lol

    Thanks!

  22. Kahil
    Member
    Posted 2 years ago #

    I've been able to pull custom fields from specific posts...just not from all posts in a set category and then output the info into separate rows...

  23. Kahil
    Member
    Posted 2 years ago #

    Oh... and I can't figure out how to make the category setting modular...so I don't have to set the tags to a specific category number or slug. As I mentioned, through the use of a plugin, I can assign categories to a page...so I suppose its worth trying to see if the category value can be called that....just don't know if it will work correctly since assigning categories to pages isn't standard.

    Thanks again for helping. I know its a bit complex with all these little "ifs" in there... If we can figure this out, someone should make it into a plugin.

  24. deepbevel
    Member
    Posted 2 years ago #

    I'm working with it.

  25. deepbevel
    Member
    Posted 2 years ago #

    I got your output to work with my query on my page but couldn't test it with your style, nor could I pull any meta. Beleive it or not I've never used custom feilds and have no idea how to make one, so I can't test that. This should be interesting.
    kahiltest1

    Of course you'll have to assign the category id, posts per row, posts per page.

    here's what I got using my own posts and css but with your output code. Never mind the "Category 3" above, that's a seperate query, nothing to do with your project. I imagine meta would appear instead of titles if I had custom feilds..?
    demo
    the query and your output are at the bottom of the page, under "Category 4" you'll see your breif case images.

  26. deepbevel
    Member
    Posted 2 years ago #

    okay, made a custom feild, it shows the value "here's a custom feild?" Just had to change the name of the feild to "test" which is what I called my very first custom feild.

    <h2><?php
    		  	$pagename = get_post_meta($post->ID, "test", $single = true);
    			if ($pagename != '') { echo $pagename; } else { the_title(); }
    		  ?></h2>
    
    <?php

    the other 3 posts in my demodon't have custom feilds, so they return the title, as per your code.

  27. deepbevel
    Member
    Posted 2 years ago #

    new code:
    revised

    css for post rows:

    .post_row { width: 100%; clear: both;}
    .post_class { width: 40%; float: left; overflow: hidden; margin: 10px;}
  28. deepbevel
    Member
    Posted 2 years ago #

    made it work in my theme (twentyten)
    gallery-page-test
    I added meta values with links to full content to all 4 posts in the query .
    Not sure if you intend to show the same 4 posts below as full content like it is currently.

  29. deepbevel
    Member
    Posted 2 years ago #

    just discovered, if you change this:

    <?php } else {
       // Code for no posts found
    }
    ?>

    to this:

    <?php } else {
       // Code for no posts found
    }
    ?>
    
    <?php wp_reset_query(); ?>

    then you can move the content area:

    <div id="container">
    			<div id="content">
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
           	 <?php the_content(); ?>
          	<?php endwhile; endif; ?>
    
    </div>
    </div>

    above or below the query, and it won't pull and duplicate the same content as the query. The reset kills the previous query.

    Here it is on my test site with the contet area above.

    demo

  30. deepbevel
    Member
    Posted 2 years ago #

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

    Each row is from a different post in that category

    We can do each row as a category, just add a query for each.

Topic Closed

This topic has been closed to new replies.

About this Topic