Home › Forums › General Issues › Custom fields not loaded when using parameter in URL
Hello,
I use ACF v. 6.0.6 and I have a strange problem, which occurs when using the ‘posts_join’ and ‘posts_where’ hook in combination with a parameter in the URL.
I have a custom taxonomy “height” where I retrieve posts of a custom post type “person”. The URL looks like this: https://example.com/height/180cm/. Each post has a lot of custom fields. Everything works fine, and the custom fields are displayed correctly.
Now I would like to filter the posts by gender (male and female persons) using a parameter in the URL -> https://example.com/groesse/180cm/?g=Er (respectively ?g=Sie).
For this I have the following code in functions.php (for test purposes I only filtered the men):
add_filter('posts_join','custom_join');
function custom_join($join){
global $wpdb;
$table = $wpdb->prefix."postmeta";
if (is_tax()) {
$join .= "LEFT JOIN $table ON $wpdb->posts.ID = $table.post_id";
}
return $join;
}
add_filter('posts_where', 'gender' );
function gender ( $where )
{
if (isset($_GET['g'])) {
if ($_GET['g'] === 'Er') {
global $wpdb;
$where .= " AND $wpdb->postmeta.meta_key = 'gender' AND $wpdb->postmeta.meta_value = 'He' ";
}
}
return $where;
}
In fact, the posts are now correctly filtered into male and female, here is the query from Query Monitor:
SELECT wp_posts.ID
FROM wp_posts
LEFT JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE 1=1
AND wp_posts.post_type = 'acf-field'
AND ((wp_posts.post_status = 'publish'))
AND wp_posts.post_name = 'field_5f400266755b3'
AND wp_postmeta.meta_key = 'geschlecht'
AND wp_postmeta.meta_value = 'Er'
ORDER BY wp_posts.menu_order ASC, wp_posts.post_title ASC
LIMIT 0, 1
However, custom fields are no longer loaded now! What can this be related to?
Thank you very much in advance for your help!
Andreas
Please don’t let the different spellings of the parameters confuse you, I forgot to unify them when translating, sorry 😉
The problem was solved after examining my code again. I’ve worked with a custom table in the past. Due to difficulties I had retrieved the fields with post_name (‘field_5f4005dc755c5’) and not with the field name (‘deathdate’). I have now re-saved the posts. After saving, I could now retrieve the field values with the field name again, and now the custom fields appear in the taxonomy output! Although I don’t know why it was related to the URL parameter, everything works now. Please excuse the confusion 😉
You must be logged in to reply to this topic.
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.