Home › Forums › General Issues › WP_Query and custom field of custom type
I have created a new custom field type named ‘expertise’. It is a hierarchical taxonomy, with custom from ends with nice icons and some javascript to use a ‘tick’ icon when an entry is selected.
I am using V4 at this stage.
My new custom post type ‘projects’ has several fields – the relevant ones are:
1) field of field type ‘expertise’ (field name ‘expertise’) – my custom field type
2) field of field type ‘Taxonomy’, field name ‘prj_region’. This uses the taxonomy named ‘regions’
I want to filter by either one or both of these taxonomies in the archive view.
I understand that WP_Query has two distinct methods for filtering by taxonomy fields vs text fields. Taxonomy fields use the ‘tax_query’, and text fields just use ‘meta_query’ elements of the argument array.
In my theme directory , I created ‘archive-projects.php’ with this code:
<?php
/**
* Template Name: Page of Projects
*/
$args=array(
'post_type' => 'projects',
'posts_per_page' => -1,
'tax_query' => array(
array(
/* SEE BELOW FOR TWO CASES OF INITIALISATION OF $args['tax_query'][] -- either:
'taxonomy' => 'regions', //OR 'taxonomy' => 'expertise',
'terms' => array(733,1196), //OR 'terms' => array(1222,1223),
*/
),
),
);
$my_query = null;
$my_query = new WP_Query ();
$my_query->query($args);
if ($my_query->have_posts ()) {
while ( $my_query->have_posts() ) {
$my_query->the_post ();
$custom_fields = get_fields();
?><h3><?php the_title();?></h3>
<p><?php echo $custom_fields['prj_details'];?></p>
<? if (is_array($custom_fields['prj_region'])) {?>
<p><strong>Region(s):</strong><?php echo implode("; ",$custom_fields['prj_region']);?></p> <?php
}
if (is_array($custom_fields['prj_region'])) { ?>
<p><strong>Expertise:</strong><?php echo implode("; ",$custom_fields['expertise']);?></p> <?php
}
}
wp_reset_postdata();
} else {
?><p>No projects found.</p><?php
}
wp_reset_query ();
?>
When I use the following $args, I get the following output:
'tax_query' => array(
array(
'taxonomy' => 'regions',
'terms' => array(733,1196),
),
),
gives:
New 2016-02-03
Details of third project
Region(s):1196
Expertise:1222
test
test
Region(s):733
Expertise:1222
Online Store Development
We want to sell something online, and we have a marketing plan - but we just need someone to help out with the technololgy.
Region(s):1196
Expertise:1222; 1223
however, when I try to filter by the field which is a custom field type I get nothing:
$args[‘tax_query’] of:
'tax_query' => array(
array(
'taxonomy' => 'expertise',
'terms' => array(1222,1223),
),
),
gives:
No projects found.
It is demonstrated by the output above that some of the records so have expertise values of 1222 and 1223.
I looked at the wp_postmeta table and found the data stored in the same way for expertise AND prj_region:
meta_id post_id meta_key meta_value
32335 5457 expertise a:1:{i:0;s:4:"1222";}
31770 5403 expertise a:2:{i:0;s:4:"1222";i:1;s:4:"1223";}
32333 5457 prj_region a:1:{i:0;s:4:"1196";}
and the term ids are stored in the same place:
SELECT * FROM wp_terms
;
term_id name slug term_group
1223 Website Development website-development 0
1222 Website Design website-design 0
1196 Australia Wide australia-wide 0
733 Melbourne melbourne 0
and both ‘regions’ and ‘expertise’ are in the wp_term_taxonomy tables.
I also tried the ‘normal’ (non-taxonomy) method with ‘meta_query’=>array(…) as one of the $arg elements, but it did not work either.
My custom field definition looks like this:
What method should I use to filter by a custom field of a custom field type?
I’m not sure how you’re custom field type works, is it actually updated the post terms the way the ACF Taxonomy field does? In ACF4 this in done on line 238 of core/fields/taxonomy.php
The topic ‘WP_Query and custom field of custom type’ 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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.