WordPress.org

Ready to get started?Download WordPress

Forums

PageOfPosts - page template displaying posts based on custom field values (13 posts)

  1. mistercyril
    Member
    Posted 3 years ago #

    Hello,

    I am setting up a custom page template for which the background and content will be defined by the custom field values of the page using the template.

    I am trying to generate 2 lists of posts, one from category A and the other from category B, the page's custom fields being "category" and "category-news" and the respective values being the category slugs.

    I've gotten this far thanks to the WP Codex, but i can't get it to work with the second category :

    <div id="page-items-list">
          <ul>
    <?php
    if (is_page() ) {
    $category = get_post_meta($posts[0]->ID, 'category', true);
    }
    if ($category) {
      $cat = get_cat_ID($category);
      $post_per_page = 18; // -1 shows all posts
      $do_not_show_stickies = 1; // 0 to show stickies
      $args=array(
        'category__in' => array($cat),
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' => $post_per_page,
        'caller_get_posts' => $do_not_show_stickies
      );
      $temp = $wp_query;  // assign orginal query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query($args);
      if( have_posts() ) :
    		while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    
              <li><div style="float:left;margin-bottom:10px;"><div class="thumblist"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a></div></div>
              </li>
    
        <?php endwhile; ?>
        </ul>
        </div><!-- .page-items-list -->
      <?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    
    	<?php endif; 
    
    	$wp_query = $temp;  //reset back to original query
    
    }  // if ($category)
    ?>

    If I copy this underneath and change get_post_meta($posts[0]->ID, 'category', true); to get_post_meta($posts[0]->ID, 'category-new', true); it doesn't find any posts...

    What am I doing wrong here?

    This may not be the smartest way to do this but my PHP skills are pretty weak so any help would be great.

    Thank you,
    C.

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    what is the value of $posts[0]->ID for the second copy?

    you have done some custom query before this, so it might not be the original page id.

    to reset this, before your second part, try and add a line with

    wp_reset_query();

    http://codex.wordpress.org/Function_Reference/wp_reset_query

  3. mistercyril
    Member
    Posted 3 years ago #

    @ alchymyth - thanks for responding so fast ;-)

    For the second copy the only thing I changed is: get_post_meta($posts[0]->ID, 'category-new', true);
    I'm trying to call the custom field called "category-new" for which the value in my test page is "sony-news-events" (category slug).

    I tried adding wp_reset_query(); just after } // if ($category) at the end of the first part but the second part is still saying "nothing found".

    I though the $temp variable was doing pretty much the same thing by re-instating the original page query at the end of my custom query. Am I wrong about this too?

  4. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    a few ideas for troubleshooting, to help you to find out where the code runs empty:

    - do you get posts if you use the second query first?
    i.e. if you use 'category-news' in the first query?

    - add 'echo' or 'var_dump()' at various positions to see if the variables contain what you expect;

    for instance, for your second query, does: echo $posts[0]->ID; show the page id?

    is if ($category) true for the second query ?
    what is the output of echo $category; at this point?
    should there be posts for this category?
    do you get the 'Not Found' message?

  5. mistercyril
    Member
    Posted 3 years ago #

    Ok, so you're right about trying the second query first, I should have tried that.

    The result is: nothing found

    So apparently i'm not using this query in the right way. I thought that this script would call whatever custom field label was being specified in the place of 'category' in get_post_meta($posts[0]->ID, 'category', true);, because that's what I need.

    Now i'm clueless...

  6. mistercyril
    Member
    Posted 3 years ago #

    Actually, on second thought, this script works as it should.

    I just need to be able to run it twice with a different category each time.

  7. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    The result is: nothing found

    does the loop output the 'Not Found' or is there nothing?

    double check:

    do you have a custom field with the key 'category-new' ?

    for your page, is this custom field filled with a value of a category slug that exists and has posts ?

    in your code, you could treplace this part:

    if (is_page() ) {
    $category = get_post_meta($posts[0]->ID, 'category', true);
    }

    with thsi extended part (which includes the code to show two main varibles):

    if (is_page() ) {
    echo 'page id: ' . $posts[0]->ID . '<br/>'; // CHECK
    $category = get_post_meta($posts[0]->ID, 'category', true);
    echo 'custom field value: ' . $category . '<br/>'; // CHECK
    }

    (for both queries)

    when youget this output, what is the difference between the one query that works, and the one that does not?
    is the output as expected?

  8. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    just saw this:

    Actually, on second thought, this script works as it should.

    exactly, that was one point of the tests.
    everything else is to find out where the second category stops/fails working.

  9. mistercyril
    Member
    Posted 3 years ago #

    1. The loop outputs "Not Found" (but there are plenty of posts in the category i'm trying to call).

    2 & 3. I double checked the custom field label and content (category slug), they all check out.

    4. I replaced the code as you said (thank you by the way) and the output confirms the same page id as the first one and outputs the right category slug but where it should display the posts it still says "Not Found"... this is weird.

    Do you have any other thoughts?

  10. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    does the category slug convert into the right category id:

    $cat = get_cat_ID($category);

    you could try to echo the variable $cat:

    $cat = get_cat_ID($category);
    echo 'cat-id: ' . $cat;
  11. mistercyril
    Member
    Posted 3 years ago #

    Ahaaa!

    The cat-id output is "0" which needless to say is not what it should be showing. So that would explain why it can't find any posts.

    I did a little testing by replacing the category slug with its id to see what happens. Heres the result:

    custom field value: 88
    cat-id: 0

    So the question is "why is it not getting the defined category id?"

  12. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    http://codex.wordpress.org/Function_Reference/get_cat_ID
    this function might be 'serious' about using the name, and not accepting the slug, if the category name is be more than one word.

    instead:

    $cat = get_cat_ID($category);

    try to use:

    $cat = get_term_by('slug', $category, 'category');
    $cat = $cat->term_id;

    http://codex.wordpress.org/Function_Reference/get_term_by

  13. AARONDAS
    Member
    Posted 3 years ago #

    Hello,

    I am using the fields plugin, and it works great. I am using the checkbox function, and I have 4 items. Playstation, xbox, wii, and PC; and I am displaying it in my template like this echo fs_get_meta('game-review-platform',$single=true, $post->ID);. It will display only one platform, even though I have 3 checked. How can I get it to display all of the checked boxes?

    Thanks in advance,

    Aaron

Topic Closed

This topic has been closed to new replies.

About this Topic