Support » Plugins » Hacks » List post with a particular custom field

  • Hi

    First i’d like to say i don’t speak very well english, i hope you’ll understand

    I’m working on a website where i can choose a region on my post.

    I’ve created a map, and when you click on a particular region, it’s supposed to list all the post from that region. But it doesn’t work…

    Each post has a departement
    A region = 2 or more departements

    For exemple

    Region : Alsace
    Departements :
    * 67 Bas-Rhin
    * 68 Haut-Rhin

    If i click on my map on Alsace, my adress is :

    I’ve try this

    while (have_posts()) : the_post();
    	$cus = get_post_custom();
    	$region = $cus['cp_dpartement'][0];
    	switch ($region)
    			case 'Bas-Rhin' :
    			case 'Haut-Rhin' :
    ( i have lot of other case, i don't write them to be shorter )
    here I display the post

    I’m sure my code is not good, as i list all my post and display only the one i need. But some aren’t displayed, and i think it’s because of pagination.

    I list 50 post, but only 3 are from the request region
    first post display good
    second and third post are in page 2 => they aren’t displayed good

    so my questions are

    how can i do something like this ?


    Is there a way to change this
    into this

    Is there a way to paginate ?

    You can visit my site on ( map is on home page )

    You can maybe undestand better region and departement by watching this

    thank you


Viewing 5 replies - 1 through 5 (of 5 total)
  • esmi


    Forum Moderator


    What is the meta-key?


    I tried query_posts('meta_value=Bas-Rhin,Haut-Rhin'); and query_posts('meta_value=Bas-Rhin,Haut-Rhin&meta_key=cp_dpartement');

    but it didn’t worked

    I’ve done this and it’s working :

    $querystr = "
        SELECT wposts.*,wpostmeta.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND wpostmeta.meta_key = 'cp_dpartement'
        AND ($dep)
        AND wposts.post_status = 'publish'
        AND wposts.post_type = 'post'
        ORDER BY wposts.post_date DESC
     $pageposts = $wpdb->get_results($querystr, OBJECT);
    foreach($pageposts as $post):

    But i don’t know how to paginate this, and now to have my adress



    Forum Moderator

    Just a thought but why don’t you have a custom field called “region” that you can use to enter values such as “alsace”? It will make the custom query so much easier to compose and easily allow pagination.

    Because I don’t want my users have to choose the region and the departement when they create a post ^^ ( it’s a second hand sale site where people can post their own items )



    Forum Moderator

    How about:

    $region  = $_GET['region'];
    $listed_regions = array( 'alsace', [ ... ] );
    if ( !in_array( $region, $listed_regions ) )  die( 'No such region!' );
    switch ($region) {
        case 'alsace' :
        $region_court = array('Bas-Rhin',  'Haut-Rhin');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if( isset( $region_court) ) :
    foreach( $region_court as $departement ) :
    $args= array(
        'category__not_in' =>3,
        meta_key' = > 'cp_dpartement',
        'meta_value' => $departement,
        'paged' => $paged
    [ The Loop ]


Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘List post with a particular custom field’ is closed to new replies.