WordPress.org

Forums

Displaying posts fromcategorie based on custom field (3 posts)

  1. Aysseline
    Member
    Posted 7 years ago #

    I want to display post from a categorie on a page, but only thoses with a certain custom field. I trying this custom query but all the posts in that category are displayed:
    <?php $actus = new WP_Query("meta_key=Esat&meta_value=Hennebont&cat=4&showposts=4"); while($actus->have_posts()) : $actus->the_post();?>

    I don't understand why this can't work :(

  2. WPQuestions.com
    Member
    Posted 7 years ago #

    Try making a seperate query and adding that to your wp_query object this way:

    $querystring = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'Esat'
        AND wpostmeta.meta_value = 'Hennebont'
        AND wposts.post_status = 'publish'
        AND wposts.post_type = 'post'
        ORDER BY wposts.post_date DESC
     ";
    
     $myposts = $wpdb->get_results($querystring, OBJECT);

    You then have to setup $myposts and loop through the results.

  3. Aysseline
    Member
    Posted 7 years ago #

    Thanks for your reply. That run but not all:
    - That display one post only (but I have two with this metakey) maybe I need to put "showposts number" elsewhere
    - The read more link reload the page itself and doesn't point to permalink of the post

    You then have to setup $myposts and loop through the results

    I think the answer is here but don't know exactly how to deal with. My first code:

    <h4>Actualité Alter Ego</h4>
    <?php $actus = new WP_Query("cat=4&showposts=4"); while($actus->have_posts()) : $actus->the_post();?>
    <img class="thumb" src="<?php bloginfo('template_url'); ?>/img/thumbs/<?php echo get_post_meta($post->ID, "Thumb", true); ?>" alt="<?php the_title(); ?>" width="60" height="40" />
    <h5><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h5>
    <?php the_content_limit(75, "Lire la suite &raquo;"); ?>
    </div><div class="filethred"></div>
    <?php endwhile; ?>
    <div class="colrightbot"></div>
    </div><!-- END content colright-->
    <div class="post"><!-- START content post-->
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <div class="entry"><?php the_content(); ?></div>
    <?php endwhile; ?>

    And now with your changes:

    <h4>Actualité de Alter Ego</h4>
    <?php $querystring = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'Esat'
        AND wpostmeta.meta_value = 'Hennebont'
        AND wposts.post_status = 'publish'
        AND wposts.post_type = 'post'
        ORDER BY wposts.post_date DESC
     ";
    
     $myposts = $wpdb->get_results($querystring, OBJECT);?>
    <div class="actus">
    <img class="thumb" src="<?php bloginfo('template_url'); ?>/img/thumbs/<?php echo get_post_meta($post->ID, "Thumb", true); ?>" alt="vignette" width="60" height="40" />
    <h5><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h5>
    <?php the_content_limit(75, "Lire la suite &raquo;"); ?>
    </div><div class="filethred"></div>
    <div class="colrightbot"></div>
    </div><!-- END content colright-->
    <div class="post"><!-- START content post-->
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <div class="entry"><?php the_content(); ?></div>
    <?php endwhile; ?>

    I want to display the thumb and content_limit of the post and also show 4 posts in that cat. I trying different code with always error :(

Topic Closed

This topic has been closed to new replies.

About this Topic