• I’m using a plugin called WP-Geolocation-JS which gets geolocation details from a site visitor and allows you display that information using shortcodes, very straightforward.

    I am using that data to auto detect the site visitor’s location and filter search results (this is for a business directory website) automatically based on that city. This is to keep someone in Virginia who is searching for a business having to sift through businesses in California.

    Now on to the question:

    I am trying to query by city, which I am getting from this plugin, and checking it against my custom meta key for city that my posts have.

    In short it should work something like this:

    $geocity = do_shortcode(‘[mmjs-city]‘);
    query_posts(“meta_key=city&meta_value=$geocity&post_type=listings”);

    But this doesn’t work. I know the short code is working, I can echo that out separately so that’s not the issue. I also know my query posts is working because just to test I manually set $geocity = to my city (the exact same as it displays when I echo out the shortcode for the plugin) and it works just fine.

    So the issue, I think atleast, is that the query doesn’t accept a value that outputs from a shortcode?

    It’s just baffling to me because I can say “echo do_shortcode(‘[mmjs-city]‘);” and get a value, for example, “Atlanta” and then set $geocity = “Atlanta” and (let’s assume I’m in Atlanta) when I query my posts for that it works. But when I try setting $geocity = do_shortcode(‘[mmjs-city]‘); (which I just proved that the short code has a value of “Atlanta”) and I query my posts it doesn’t work.

    Is there a way to get this to work? Is there another way to do what I am trying to do.

    I would really appreciate any help anyone has to offer. Thank you!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter mshelton15

    (@mshelton15)

    Anyone got atleast a clue?

    I wonder if the shortcode is returning some non-visible/non-printable character so that when you echo the value you don’t see it but it messes up the query? You might try echoing strlen($geocity) to find out.

    Thread Starter mshelton15

    (@mshelton15)

    @vtxyzzy Thanks for the reply.

    I just tried echo strlen($geocity) and it output 68

    I don’t know what strlen does but from my googling it looks like 68 is the length of the string which I interpret as meaning that the value output is 68 characters long. Is that correct?

    If so, then that’s definitely the issue since the city name I am testing with is 10 characters.

    Any way you might know of to fix that or do something different to achieve the same result?

    Well the question now: What is the value ‘padded’ with?

    Try trimming whitespace with $geocity = trim(do_shortcode(‘[mmjs-city]‘));

    And then check the length again.

    Thread Starter mshelton15

    (@mshelton15)

    I did $geocity = trim(do_shortcode(‘[mmjs-city]’)); and still got 68 for the length.

    I also tried to get rid of non-printable characters with:

    $geocity = preg_replace( ‘/[^[:print:]]/’, ”, $geocity );

    and still got 68 for the length.

    OK – I understand what is happening now.

    The shortcode returns javascript which gets sent to the browser when you echo $geocity. Here is the script (length 68):

    <script language="javascript">document.write(geoip_city());</script>

    The js is executed by the browser and writes the city name to the document. So, the output of the js is not available to PHP.

    What you are trying to do cannot be done.

    Thread Starter mshelton15

    (@mshelton15)

    Thanks so much for the help.

    Bummer that it won’t work though. And there isn’t any other way that you might know of? Maybe a completely different approach?

    Thanks again for you help, I really appreciate it!

    I have not looked for a way to do that. You would need to find a plugin that uses php code, not JS.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Query Posts With Value From Shortcode?’ is closed to new replies.