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 : http://www.mysite.com/carte/?region=alsace

    I’ve try this

    query_posts('category__not_in=3');
    while (have_posts()) : the_post();
    	$cus = get_post_custom();
    	$region = $cus['cp_dpartement'][0];
    	switch ($region)
    			{
    			case 'Bas-Rhin' :
    			$region_court='alsace';
    			break;
    
    			case 'Haut-Rhin' :
    			$region_court='alsace';
    			break;
    ( i have lot of other case, i don't write them to be shorter )
    }
    if($region_court==$_GET['region']){
    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 ?

    query_posts('meta_value=Bas-Rhin,Haut-Rhin');

    Is there a way to change this
    http://www.mysite.com/carte/?region=alsace
    into this
    http://www.mysite.com/carte/alsace

    Is there a way to paginate ?
    http://www.mysite.com/carte/alsace/p/2

    You can visit my site on http://www.vieuxbicloo.com/ ( map is on home page )

    You can maybe undestand better region and departement by watching this http://fr.wikipedia.org/wiki/D%C3%A9partements_fran%C3%A7ais_class%C3%A9s_par_r%C3%A9gion#France_m.C3.A9tropolitaine

    thank you

    Thomas

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

    (@esmi)

    Forum Moderator

    query_posts('meta_value=Bas-Rhin,Haut-Rhin');

    What is the meta-key?

    Hi

    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):
    ...
    endforeach

    But i don’t know how to paginate this, and now to have my adress
    http://www.mysite.com/carte/alsace

    esmi

    (@esmi)

    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 )

    esmi

    (@esmi)

    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');
        break;
    
        [...]
    }
    $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
    );
    query_posts($args);
    [ The Loop ]
    wp_reset_query();
    endforeach;
    endif;

    [NOT TESTED]

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