Home › Forums › Add-ons › Repeater Field › Dynamically get meta_key fix for esc_sql() change in wp 4.8.3
Hey guys
from this url : https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
I have everything working with a decently complex set of filters, for various CPT’s
One thing I can’t figure out, is how to get this dynamically for various repeater fields.
In the example below, this works for the one repeater field of ‘feelings’ and a sub field of ‘feeling’
I can’t figure out what to use in the IF statement to populate these for each type.
I have ‘ feelings_$_feeling ‘ ‘ medical_use_$_medical ‘ and a few others.
Any help would be huge. I have tried 100 things and nothing is working for me.
add_filter('posts_where', 'my_posts_where');
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'feelings_$", "meta_key LIKE 'feelings_%", $where);
return $where;
}
Hey guys, I almost had it figured out. I don’t know why I didn’t think of just using a $GET. So as long as 1 filter is selected at a time, it now works. But the second that I select a filter, and a medical_use (url of something like ” strains?feelings_$_feeling=Euphoric&medical_use_$_medical=Seizures ”
Does anyone have any idea how I can get this to work for any meta_query in the URL?
add_filter('posts_where', 'my_posts_where');
function my_posts_where( $where ) {
if( isset($_GET['feelings_$_feeling']) ) {
$where = str_replace("meta_key = 'feelings_$", "meta_key LIKE 'feelings_%", $where);
return $where;
}
if( isset($_GET['medical_use_$_medical']) ) {
$where = str_replace("meta_key = 'medical_use_$", "meta_key LIKE 'medical_use_%", $where);
return $where;
}
return $where;
}
I am trying to figure out how I can just use $_get, and pull the array.
Using this
// If you have the following URL :
// http://www.example.com/test.php?a=10&b=plop
// Then $_GET will contain :
array
'a' => string '10' (length=2)
'b' => string 'plop' (length=4)
From https://stackoverflow.com/questions/5415224/how-to-set-get-variable
So, this should loop through each parameter and value in the URL.
However; if I understand this filter, it is only pulling part of the value, and I can’t figure out what this is actually populating for $rootkey. I can’t figure out how to see that ECHO.
$rootkeys[] = $_GET;
foreach($rootkeys as $rootkey) {
$where = str_replace("meta_key = 'feelings_$", "meta_key LIKE 'feelings_%", $where);
}
return $where;
Ok, so I got this far…..
$keya outputs feelings_$ and medical_use_$ which is CORRECT….. But… this still doesn’t work.
$rootkeys[] = $_GET;
foreach($rootkeys as $rootkey) {
foreach($rootkey as $key => $value) {
$partialkey = explode("$",$key);
$keya = $partialkey[0].'$';
$where = str_replace("meta_key = $keya", "meta_key LIKE 'feelings_%", $where);
}
}
return $where;
Ok, got it…. finally…
$rootkeys[] = $_GET;
foreach($rootkeys as $rootkey) {
foreach($rootkey as $key => $value) {
$partialkey = explode("$",$key);
$keya = $partialkey[0];
$keyb = 'meta_key = \''.$keya.'$';
$keyc = 'meta_key LIKE \''.$keya.'%';
$where = str_replace($keyb, $keyc, $where);
}
}
return $where;
The topic ‘Dynamically get meta_key fix for esc_sql() change in wp 4.8.3’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.