How can I get a taxonomy by custom post type fields
-
Hi , I’m developing a wordpress site about real state, I have a custom post type called “Property” and a custom taxonomy(attached to property post type) “Location” (Kendall, Boca raton , Del Ray beach etc), I would like to grab or display only the Locations wich Property custom field “property_type” is value “floorplan”. This is what’s I’ve been trying. In other words… need to be able to see locations only who have “floorplan” value.
this code is within the loop that I already started
This is the code to display buttons with all existing location taxonomies.
<div class="style-filter"> <!-- Category Filter --> <dl class="group maingroup"> <dt></dt> <dd> <ul class="filter group" id="isofilters"> <li><a href="JavaScript:void(0);" data-filter="*" class="selected"><?php _e('ver todas','advertica'); ?></a></li> <?php $locations=get_categories($args); foreach ($locations as $category) { $loc_name=$category->name; $loc_slug=$category->slug; ?> <li><a href="JavaScript:void(0);" data-filter="<?php echo $loc_slug; ?>"><?php echo $loc_name; ?></a></li> <?php } ?> </ul> </dd> </dl> <!-- Category Filter --> </div>
-
I see you are building filters for use with isotope here. I have been down this road.
Are you trying to filter the taxonomy terms themselves or filter the posts by taxonomy term + custom field?
The posts by taxonomy term + custom field, actually is getting all existing taxonomies.
So when you click a location filter in the list, you want it to return only “properties” posts that have a custom field named “property_type” with a value of “floorplan” ?
Apologies, just making sure I understand the question.
Yes, I’ve done that with the main loop, actually I’m getting propety post types with floorplan value.
<div class="project_wrap clearfix"> <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?> <?php $args = array( 'post_type' => 'property', 'posts_per_page' => -1 , 'meta_key' => 'property_type', 'meta_value' => 'floorplan', 'paged' => $paged); query_posts($args); $args = array( 'taxonomy' => 'property_location' ); $data_type = null; //For display data-type class $data_id = 0; //For display data-id class ?> <div id="wrap" class="c
This code is below of the “for each” mentioned in the previous entry, and is actually getting the floorplan entries.
So out of that pool of posts you want to sort the results by location using isotope? If so, you need to get the location value and add it to the (list of) class(es) for each item in the loop then set the data-filter attribute of the filter to target items with that value.
Something like so:
$('#filters').on( 'click', 'a.filter', function(evt) { evt.preventDefault(); var filterValue = $(this).attr('data-filter'); $container.isotope({ filter: filterValue }); });
P.S. you should probably be using WP_Query($args) instead of query_posts($args)
https://www.lilianamartinezrealtor.com/zonas-de-inversion/ this is without the changes that I want to add, you will see all properties and all locations, are the red buttons the isotope thing?
I see, you are using a paid theme “advertica” which loads the jquery ‘isotope” filtering plugin. Look familiar? https://isotope.metafizzy.co/
In the case that you were building this from scratch I could help you but I think this case it’s best to contact support for the makers of the advertica theme. Without knowing that particular paid theme intimately I cannot tell you how that particular functionality was implemented.
Thank you so much for your time, Have a wonderful day.
I think is not about the isotope jquery, ia about what values you pass trough the for each loop, there’s got to be a way to get or query by a custon post field.
Ah I see, I misunderstood.
You use WP_Query and pass it a tax_query and meta_query like so:
first define an array of your tax terms using get_terms:
https://codex.www.ads-software.com/Function_Reference/get_terms
Then your query would be something like:$args = array( 'post_type' => 'property', 'tax_query' => array( array( 'taxonomy' => 'property_location', 'field' => 'slug', 'terms' => $arrayofterms, // this is an array you must define )), 'meta_query' => array( array( 'key' => 'property_type', 'value' => 'floorplan', )), ); $myloop = new WP_Query($args); if($myloop->have_posts()):while($myloop->have_posts());$myloop->the_post();
and finish your loop.
or you may want to run the query once for every term in a foreach loop:
$terms = get_terms('property_location'); foreach($terms as $term): $args = array( 'post_type' => 'property', 'tax_query' => array( array( 'taxonomy' => 'property_location', 'field' => 'slug', 'terms' => $term, )), 'meta_query' => array( array( 'key' => 'property_type', 'value' => 'floorplan', )), ); $myloop = new WP_Query($args); if($myloop->have_posts()):while($myloop->have_posts());$myloop->the_post(); //do your loop markup endwhile; endif; endforeach;
Sorry caught my mistake too late to edit – corrected query
$terms = get_terms('property_location'); foreach($terms as $term): $args = array( 'post_type' => 'property', 'tax_query' => array( array( 'taxonomy' => 'property_location', 'field' => 'slug', 'terms' => $term->slug, ),), 'meta_query' => array( array( 'key' => 'property_type', 'value' => 'floorplan', ), ), ); $myloop = new WP_Query($args); if($myloop->have_posts()):while($myloop->have_posts());$myloop->the_post(); //do your loop markup endwhile; endif; endforeach;
* code is not tested, sorry if it needs adjustment
I got confused, where do you suggest to add your code?
this is my code.
<?php get_header(); /* * GM Template name: Property 4-col Pre construct */ ?> <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post(); ?> <?php $skt_port_filter_hide = sketch_get_option($advertica_shortname.'_hide_pro_filter'); $pagetitle = get_post_meta($post->ID, "_skt_pagetitle_metabox", true); $pagetitle = ((isset($pagetitle) && $pagetitle !="") ? $pagetitle : '1' ); ?> <?php if($pagetitle == 1) { ?> <div class="bread-title-holder"> <div class="bread-title-bg-image full-bg-breadimage-fixed"></div> <div class="container"> <div class="row-fluid"> <div class="container_inner clearfix"> <h1 class="title"> <?php the_title(); ?> </h1> <?php if(sketch_get_option($advertica_shortname."_hide_bread") == 'true') { if ((class_exists('advertica_breadcrumb_class'))) {$advertica_breadcumb->custom_breadcrumb();} } ?> </div> </div> </div> </div> <?php } ?> <!-- #Container Area --> <div id="container" class="clearfix"> <div class="container post-wrap"> <div class="row-fluid"> <!-- Content --> <div id="content" class="span12"> <div class="post project-temp4" id="post-<?php the_ID(); ?>" > <!-- Project Wrap --> <div class="project_wrap clearfix"> <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?> <?php $args = array( 'post_type' => 'property', 'posts_per_page' => -1 , 'meta_key' => 'property_type', 'meta_value' => 'floorplan', 'paged' => $paged); query_posts($args); $args = array( 'taxonomy' => 'property_location' ); $data_type = null; //For display data-type class $data_id = 0; //For display data-id class ?> <div id="wrap" class="clearfix"> <?php if($skt_port_filter_hide == "true") { ?> <div class="style-filter"> <!-- Category Filter --> <dl class="group maingroup"> <dt></dt> <dd> <ul class="filter group" id="isofilters"> <li><a href="JavaScript:void(0);" data-filter="*" class="selected"><?php _e('ver todas','advertica'); ?></a></li> <?php $locations=get_categories($args); foreach ($locations as $category) { $loc_name=$category->name; $loc_slug=$category->slug; ?> <li><a href="JavaScript:void(0);" data-filter="<?php echo $loc_slug; ?>"><?php echo $loc_name; ?></a></li> <?php } ?> </ul> </dd> </dl> <!-- Category Filter --> </div> <!-- style-filter --> <?php } ?> <div id="container-isotop" class="portfolio group four-col"> <?php // The Loop while ( have_posts() ) : the_post(); $terms = get_the_terms( $post->ID, 'property_location' ); $loc_links = array(); foreach ( $terms as $term ) { $loc_links[] = $term->slug; } foreach($loc_links as $itm){ $data_type .= $itm.' '; } $data_id++; ?> <!-- Project Box --> <div class="item project_box span3 <?php echo $data_type;$data_type =null; ?>" data-type="<?php echo $data_type;$data_type =null; ?>" data-id="id-<?php echo $data_id; ?>"> <!-- standard --> <div class="project-item" id="post-<?php the_ID(); ?>"> <?php if(has_post_thumbnail()) { $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'advertica_portfolio_image' ); $pretty_thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full'); ?> <div class="feature_image" style="position: relative;"> <img class="skin-border" src="<?php echo $image[0]; ?>" alt="Thumbnail" /> </div> <div class="title"> <a href="<?php the_permalink();?>" title="<?php the_title();?>"> <?php the_title();?> </a> </div> <div class="mask"> <?php $custom_fields = get_post_custom(); $my_custom_field = $custom_fields['property_type']; foreach ($my_custom_field as $key => $value) { //echo $key . " => " . $value . "<br />"; if ($value == "floorplan"){ echo "<div class='ribbon-wrapper-xl'><div class='ribbon-green-large'>Pre Construcción</div></div>"; } } ?> <a data-rel="prettyPhoto" class="icon-image folio" href="<?php echo $pretty_thumb[0]; ?>"></a> </div> <?php } ?> </div> <!-- standard --> </div> <!-- Project Box --> <?php endwhile; // Reset Query ?> </div> <div class="clearfix"></div> <?php wp_reset_query(); ?> </div> </div> <!-- Project Wrap --> </div> <?php endwhile; ?> <?php else : ?> <div class="post"> <h2><?php _e('Not Found','advertica'); ?></h2> </div> <?php endif; ?> </div> <!-- Content --> </div> </div> </div> <!-- #Container Area --> <?php get_footer(); ?>
we are almost there, I’ve been searching at the codex site and found this
<?php $taxonomy = 'category'; // get the term IDs assigned to post. $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); // separator between links $separator = ', '; if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) { $term_ids = implode( ',' , $post_terms ); $terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids ); $terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator ); // display post categories echo $terms; } ?>
That code above is actually doing what I want to , is getting me the taxonomies Assigned to that post, I’ve tested it by placing below the comment “The loop”
- The topic ‘How can I get a taxonomy by custom post type fields’ is closed to new replies.