WordPress.org

Ready to get started?Download WordPress

Forums

Gathering all posts with one specific custom value (2 posts)

  1. mariostella
    Member
    Posted 5 years ago #

    Hello,
    I got two problems into one. Although there seems to be a million posts out there about this, none solves my issue.

    FIRST: I need to gather all the posts having one specific custom field meta VALUE and list them in a page without any particular criteria (having the ability to decide how many to show would also be needed).
    I inserted the following code in the page.php template to call the page that lists the posts with the specific custom field value:

    <?php if (have_posts()&is_page('32')) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
     <?php the_content(); ?>
    
    <?php endwhile; ?>
    <?php endif; ?>

    and I am planning to insert the code that actually lists the posts inside the page editor in the admin panel (the function <?php the_content(); ?> calls the content of the page with the code). This brings to the second problem:

    SECOND: The code inside the post editor is not getting executed properly (using whatever plugin to execute php inside post/page editors). Some of it is executed but queries really mess up everything.

    Any idea? Thanks for anything you can come up with!

  2. mariostella
    Member
    Posted 5 years ago #

    Just an update. If you are thinking "well just apply a query!!" just like:

    $querystr = "
    SELECT $wpdb->posts.* FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    WHERE $wpdb->posts.post_status = 'publish'
    AND $wpdb->posts.post_type = 'post'
    AND $wpdb->postmeta.meta_key = 'sconto'
    ORDER BY $wpdb->posts.post_date DESC
    LIMIT 4";
    
     $pageposts = $wpdb->get_results($querystr, OBJECT);
    
    ?>
    
     <?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
       <?php setup_postdata($post); ?>
    <a href="<?php the_permalink(); ?>" rel="bookmark"><img src="http://www.mysite.com/images/grandi/7.jpg" /></a> <div class="prop-list-text"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a><br />From <?php meta('data_inizio'); ?> to <?php meta('data_fine'); ?><br /><?php meta('sconto'); ?>% discount</div>
    	<div class="clear"></div>
      <?php endforeach; ?>
    
     <?php else : ?>
        <h2 class="center">Not Found</h2>
        <p class="center">Sorry, but you are looking for something that isn't here.</p>
    
     <?php endif; ?>

    I already got that as you can see...but this is not what I need! I need to query all the posts with a specific custom field VALUE, not key such as in the above example.

    Can this be done?

Topic Closed

This topic has been closed to new replies.

About this Topic