WordPress.org

Forums

List post with a particular custom field (6 posts)

  1. Thos
    Member
    Posted 4 years ago #

    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

  2. esmi
    Forum Moderator
    Posted 4 years ago #

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

    What is the meta-key?

  3. Thos
    Member
    Posted 4 years ago #

    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

  4. esmi
    Forum Moderator
    Posted 4 years ago #

    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.

  5. Thos
    Member
    Posted 4 years ago #

    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 )

  6. esmi
    Forum Moderator
    Posted 4 years ago #

    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]

Topic Closed

This topic has been closed to new replies.

About this Topic