Using variables in Query string

Hello,

Please can you help me with this…

I’m trying to create a looper based on a custom query string

I have this query where the variable $country_code looks up the IP location and outputs a country code


$country_code = $_SERVER[“HTTP_CF_IPCOUNTRY”];
$args = array(
‘meta_key’ => ‘country’,
‘meta_value’ => $country_code,
‘meta_compare’ => ‘contains’
);
$query = new WP_Query( $args);


Then I’m using … var_dump( http_build_query( $query ) ); to generate my query string

however, when this happens, the variable $country_code is appearing as the country code of my IP e.g. GB

i need $country_code to stay as a variable so it updates depending on the users IP location.

Thanks!

Hi @jackc,

Thanks for reaching out!

Please check this thread on how you can generate a query string that you can assign a variable on it.

Hope that helps.

Hello,

I’m still struggling to show my variable $country_code as dynamic content…


the original Wp query

$country_code = $_SERVER[“HTTP_CF_IPCOUNTRY”];
$args = array(
‘meta_key’ => ‘country’,
‘meta_value’ => $country_code,
‘meta_compare’ => ‘contains’
);
$query = new WP_Query( $args);


i cant seem to make this…

$country_code = $_SERVER[“HTTP_CF_IPCOUNTRY”];

show as dynamic content, I’ve tried {{dc:global:country_code}} & {{dc:post:country_code}}

I’ve also tried tweaking the query to match the example thread…


$country_code = $_SERVER[“HTTP_CF_IPCOUNTRY”];
$args = array(
‘meta_query’ => array(
array(
‘key’ => ‘country’,
‘value’ => ‘{{dc:post:country_code}}’,
‘compare’ => ‘contains’,
‘type’=> ‘CHAR’
),
)
);
$query = new WP_Query( $args);


If you could point me in the right direction - thanks a lot.

Hi @jackc,

I would suggest you use the Custom Looper Provider where you can call a PHP function written into the functions.php of your child theme and you can use the $country_code into it. I would suggest you go through the following threads related to the custom looper.



I would suggest you go through the following article and videos on the Looper.


If you want to use the Country Code as a Dynamic Content, you need to create custom Dynamic Content. I would suggest you go through the following thread on that.

Hope it helps.
Thanks

Thanks for providing those threads but im struggling to understand how to apply them.

I need to parse a json request to my php query in order to apply the ‘custom’ looper provider, is this also post Id as in the examples?

Hi @jackc,

You can send any JSON value to the Custom Looper using the Params as shown in the following thread. If you use the get_the_ID() method in your PHP you will get the POST ID or you can send the {{dc:post:id}} through the params to get the ID in your custom looper function.

Please remember that we don’t offer any support to the custom coding or any customization, I would suggest you hire a developer who can assist you to do the customization or you can avail of our newly launched service called One, where we answer the questions beyond normal theme support.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.