display global recent post by custom field

Hi Guys,
there is something i want to use in my site but not sure how and if i can do it. but i think this can be very useful.
my site going to be directory site where people will be able to add listing, events, coupons, jobs and so on. in the main page i am going to use the global recent post widget few time to display featured listing, events, coupons and so on. Now, i would like my main page (or the whole site) to display posts in those widget sorted by the visitor's zip code for example. so if someone from New York will go to the main page he will not see featured listings or events that going to be in miami (unless he will search for it) but he will see those that around his area. My first thought was maybe to use a global variable (zip code , state or anyhtings else) that a user will enter when he first visit the site (just like many other site out there) . Then the Global Recent Post widgets on my main page will use this variable to display the posts that have the same value. i am thinking about using custom fields here as i created address fields (street , zip code, city ,....) for all my custom post type. Right now there is no option in the global recent post widget to filter by custom field but if it can be added i might be able to do what i want.
do you guys think i can do it this way? maybe other suggestions you guys have?

thank you

  • DavidM
    • DEV MAN’s Mascot

    Hi fitoussi,

    Currently, Post Indexer's capabilities are limited regarding custom post types and it doesn't take into account their custom fields or taxonomies and tags.

    In the following thread there's a request to integrate taxonomies in the Post Indexer plugin (Global Recent Posts relies on this), though I couldn't find a similar request for custom fields.

    Perhaps you might be able to achieve your desired goal using taxonomies and tags?


  • fitoussi
    • The Incredible Code Injector

    Waw, so that answers my topic https://premium.wpmudev.org/forums/topic/directory-listing-and-classifieds-tags-problem. i was pulling my hair trying to figure out why my custom tags and categories are not being indexed.
    i think this feature will be very useful. specially since many of the plugin you guys developing are based on custom post types and custom taxonomies.

    Now, as for my request i managed to work something out. just first step :

    this is part of the widget-recent-global-posts.php and you can see the part i added:

    if (count($posts) > 0){
    	echo '<ul>';
    	foreach ($posts as $post){
    	//// check zip code of post ////////
    	$zipcode = get_post_meta($post['post_id'],'zipcode',true);
    	if ($zipcode == '33314') {
    echo '<li>';

    now the widget displays only posts in zip code 33314 and because all my custom post type have same "zipcode" custom field it makes it easier. now i need to chang the "33314" to $user_zipcode when this one will have the value that a user enter when he first visit the site. but not sure how to do it yet.
    any clue about this one?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.