How to use custom fields to search for the specific posts?

My intention is to build a website with places/attractions (as posts) I would like to visit i the future. Something like “extended” Bucket list. 🙂

I created couple categories, enter couple posts… And then I realized I’m gonna have some problems with “location”. Every post should have Address, City, State, Country (provinces, territories, regions… I’m gonna deal with it later). First idea was, of course, to incorporate the location info into the post itself. search for e.g. New York would list all posts that have “City: New York”, but would list also all posts that just have mentioned New York, not “from” NY. And, from data structure point of view, the location data should be separated from posts.
Then I tried to use Tags, entering City, State, Country as Tags. Good news: it was working the way I wanted – but not looking the way I wanted. The Tags were mixed (not always the City value was on the frst place) and also mixed with other, “regular” Tags. 🙁
BTW, can I have two sets of tags?

After I done some research and “discovered” ACF – Advanced Custom Fields plugin – and it was the “Winner”. I creaated the Group “Location”, 4 fields (Address, City, State, Coutry) – everythig was ok. Wasn’t happy when I realized it doesn’t show on the front end but somehow I managed the code (although it doesn’t seems right to me?!?!?) by using Appearance/Editor, I added the following code into templet-parts -> post -> content.php

echo ‘<div style=”font-size: .9em; margin-bottom: 25px; padding: 25px;”>’;

if (get_field(‘address’)) {
$address = get_field(‘address’);
echo ‘Address: ‘.$address.'<br />’;
}

if (get_field(‘city’)) {
$city = get_field(‘city’);
echo ‘City: ‘.$city.'<br />’;
}

if (get_field(‘state’)) {
$state = get_field(‘state’);
echo ‘State: ‘.$state.'<br />’;
}

if (get_field(‘country’)) {
$country = get_field(‘country’);
echo ‘Country: ‘.$country.'<br />’;
}

echo ‘</div>’;

It was working well, showing cties, states… But just showing, they were not “linkable/clickable” to use to select all posts with the selected city…?!? I know I can make them as a link – but don’t know where to point the link?
Any idea how to use the custom fiels(s) as keywords to search through custom fields only?

I think if I can have multiple groups of tags, I can, e.g. New York for City, enter into the Location group of tags, and enter into City custom field too, and then make it the City custom field as a link to http://domain.com/tag/New-York , right?

So, how to do this? Or, if you would suggest different approach, please let me know.

Thanks for any help.

Read more here:: How to use custom fields to search for the specific posts?

Leave a Reply

Your email address will not be published. Required fields are marked *