I have a map in my website with dots that I localize the number of office per country on a map picture.
How it works is basically I have CPT as office and I create posts as city name like New York, London etc.
For example, if I have an office in New York, USA, I will create the post as New York and custom category will be country name as the USA. Also, in office CPT, I have custom fields for coordinating the dots on the map as Home_x and Home_Y.
So the outcome with the below code is like:
USA/New York
USA/Chicago
United Kingdom/London
United Kingdom/Bristol
Spain/Barcelona
Spain/Granada
My code for loop is;
<div class="map-wrapper">
<div class="map">
<?php
$terms = get_terms(array(
'taxonomy' => 'office-country',
'hide_empty' => false,
));
?>
<?php foreach ($terms as $term) : ?>
<?php
$re = explode('-', $term->name);
$args = array (
'post_type' => 'office', //
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'office-country',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$title = get_the_title();
$info = get_post_meta(get_the_ID(), '_post_info', true);
$link = get_term_meta($term->term_id, 'link', true);
?>
<a href="<?php echo $link ?>"
class="point <?php if ($term->slug === $_GET['country']) echo 'active' ?>"
style="left: <?php echo $info['home_x']; ?>px; top: <?php echo $info['home_y']; ?>px;"
data-target=".country-popup-<?php echo $term->term_id ?>">
<div class="inner"></div>
<div class="text">
<span class="name"><?php echo $re[0] ?> </span>
<span class="number"><?php echo " / ".$title; ?>
</span>
</div>
</a>
<?php
}
}
endforeach; ?>
</div>
</div>
and my custom field for CPT is;
<tr>
<th>
<label><?php _e('Home X'); ?></label>
</th>
<td>
<input type="text" name="_post_info[home_x]" value="<?php echo $info['home_x'] ?>">
</td>
</tr>
<tr>
<th>
<label><?php _e('Home Y'); ?></label>
</th>
<td>
<input type="text" name="_post_info[home_y]" value="<?php echo $info['home_y'] ?>">
</td>
</tr>
Above code works perfectly for this purpose.
But I want to change the custom taxonomy as office and post as a country name now. Instead of creating multiple posts for the city, creating a country post and adding cities as custom taxonomy is much easier. So I am trying to change below code for a new way.
I have changed the loop code as below, and create a custom field for custom-taxonomy, I also indicate it below too.
New loop;
<div class="map-wrapper">
<div class="map">
<?php
$terms = get_terms(array(
'taxonomy' => 'office-city',
'hide_empty' => false,
));
?>
<?php foreach ($terms as $term) : ?>
<?php
$re = explode('-', $term->name);
$args = array (
'post_type' => 'office-country', //
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'office-city',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$title = get_the_title();
// $info = get_post_meta(get_the_ID(), '_post_info', true);
$link = get_term_meta($term->term_id, 'link', true);
$MapY = get_term_meta($term->term_id, 'home_y', true);
$MapX = get_term_meta($term->term_id, 'home_x', true);
?>
<a href="<?php echo $link ?>"
class="point <?php if ($term->slug === $_GET['country']) echo 'active' ?>"
style="left: <?php echo $MapY ?>px; top: <?php echo $MapX ?>px;"
data-target=".country-popup-<?php echo $term->term_id ?>">
<div class="inner"></div>
<div class="text">
<span class="name"><?php echo $title; ?> </span>
<span class="number"><?php echo " / ".$re[0] ?>
</span>
</div>
</a>
<?php
}
}
endforeach; ?>
</div>
</div>
Custom field for taxonomy;
add_action( 'office-country_edit_form_fields', 'office_country_taxonomy_custom_fields', 10, 2 );
function office_country_taxonomy_custom_fields($tag) {
?>
<tr>
<th>
<label><?php _e('Home X'); ?></label>
</th>
<td>
<input type="text" name="_term_meta[home_x]" value="<?php echo get_term_meta($tag->term_id, 'home_x', true) ?>">
</td>
</tr>
<tr>
<th>
<label><?php _e('Home Y'); ?></label>
</th>
<td>
<input type="text" name="_term_meta[home_y]" value="<?php echo get_term_meta($tag->term_id, 'home_y', true) ?>">
</td>
</tr>
<?php
}
But when I applied this code, it partly works and I have no idea to change the query.
The result;
USA/New York
United Kingdom/London
Spain/Barcelona
So, it does only show posts per taxonomy but I want to show all the cities per country like below;
USA/New York
USA/Chicago
United Kingdom/London
United Kingdom/Bristol
Spain/Barcelona
Spain/Granada
Sorry, it is a long question. I hope you guys can help on this, and I hope you guys understood my broken English.
Your loop is working correctly as i got result what you wanted.
<?php
$terms = get_terms(array(
'taxonomy' => 'office-city',
'hide_empty' => false,
));
foreach ($terms as $term) :
$args = array (
'post_type' => 'office-country', //
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'tax_query' => array(
array(
'taxonomy' => 'office-city',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$title = get_the_title();
echo '<p>'. $title . '/' . $term->name . '</p>';
}
}
endforeach; ?>
Related
I'm trying to create a ACF flexible content row to display the most recent post thumbnails for a given category. However it keeps throwing a critical error and I'm not sure why.
<?php
$section_id = get_sub_field('section_id')
$categories = get_sub_field('categories');
$tags = get_sub_field('tags');
$postnum = get_sub_field('number_of_posts');
if (!is_array($categories)) {
$categories = array($categories);
}
$tags = get_field('my_tags_field');
if (!is_array($tags)) {
$tags = array($tags);
}
$args = array(
'post_type' => 'post',
'numberposts' => $postnum,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => $categories
),
array(
'taxonomy' => 'post_tag',
'terms' => $tags
)
)
);
$query = new WP_Query($args);
?>
<style>
</style>
<section class="post_row_with_thumbnails" id="<?php echo $section_id; ?>">
<div class="container-fluid">
<div class="row">
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<div class="col">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>" class="project_pics">
<h5 class="posttitle"><?php the_title(); ?></h5>
<h6 class="postdate"><?php the_date(); ?></h6>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
</div>
</section>
I have tried substituting WP_Query() with get_posts() but it gives me the same critical error.
In line 2 the ; at the end of the line, in this part:
$section_id = get_sub_field('section_id') ; THE DOT AND COMMA IS MISSING
This is the reason for the critical error.
I have this function to filter posts by fields and taxonomies. The problem is that when I use the taxonomy filter it doesn't work and it returns all the posts without filtering.
I didn't include js files because ajax is working well.
Here's the PHP in functions.php:
function post_filter()
{
$number = $_POST['number'];
$autor = $_POST['autor'];
$args = array(
'post_type' => 'publicaciones',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'numero',
'meta_value'=> $number
);
if( isset( $autor ) )
$args['tax_query'] = array(
array('taxonomy' => 'autors',
'field' => 'id',
'terms' => $autor)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
//Post content
endwhile;
wp_reset_postdata();
else :
echo 'No results.';
endif;
wp_die();
}
And this is the form:
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" class="my-5" id="buscador_numero">
<div class="row">
<div class="col-3">
<?php
if( $terms = get_terms( array( 'taxonomy' => 'autors', 'orderby' => 'name' ) ) ) :
echo '<select name="autor"><option value="">Autor/a</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
endforeach;
echo '</select>';
endif;
?>
</div>
<div class="col-2">
<input type="number" class="w-100 p-2" name="number" placeholder="NĂºmero">
</div>
<div class="col-2">
<button id="btn_buscar"><span class="dashicons dashicons-search"></span></button>
</div>
</div>
<input type="hidden" name="action" value="post_filter">
</form>
Thanks in advance :)
I've been trying to get isotope.js working on a Wordpress site. I've been following this tutorial https://www.aliciaramirez.com/2014/03/integrating-isotope-with-wordpress/ and have been able to get it all functioning. For my design, I'm trying to add <div class="grid-sizer"></div> every four posts that are called. I've been referring to this question: Wrap every 4 posts in a custom wordpress loop with a div but cannot seem to figure out the proper placement for the count and i statements. Can anyone help me figure this out? Here's my loop right now:
<?php
$terms = get_terms( array(
'taxonomy' => 'solutions',
'parent' => 0
)
); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => asc,
'tax_query' => array(
array(
'taxonomy' => 'solutions',
'field' => 'term_id',
'terms' => get_queried_object()->term_id,
),
) );
$the_query = new WP_Query( $args ); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "solutions" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
$i = 0;
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
if($i%4 == 0) {
echo "<div class='grid-sizer'> </div>";
}
?>
<div class="<?php echo $termsString; ?>item">
<p class="product-image"><a href="<?php the_permalink(); ?>" ><img src="<?php the_field("product_image") ?>" alt="<?php the_title(); ?>" class="solution-image" /></a></p>
<h4 class="product-name">
<?php the_title(); ?>
</h4>
</div>
<?php $i++; ?>
<!-- end item -->
<?php endwhile; ?>
</div>
<!-- end isotope-list -->
<?php endif; ?>
Here's the resulting code - thanks for misorude's help!
<?php
$terms = get_terms( array(
'taxonomy' => 'solutions',
'parent' => 0
)
); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php
$i = 0;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => asc,
'tax_query' => array(
array(
'taxonomy' => 'solutions',
'field' => 'term_id',
'terms' => get_queried_object()->term_id,
),
) );
$the_query = new WP_Query( $args ); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "solutions" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
if($i%4 == 0) {
echo "<div class='grid-sizer'> </div>";
}
?>
<div class="<?php echo $termsString; ?>item">
<p class="product-image"><a href="<?php the_permalink(); ?>" ><img src="<?php the_field("product_image") ?>" alt="<?php the_title(); ?>" class="solution-image" /></a></p>
<h4 class="product-name">
<?php the_title(); ?>
</h4>
</div>
<?php $i++; ?>
<!-- end item -->
<?php endwhile; ?>
</div>
<!-- end isotope-list -->
<?php endif; ?>
For a single page I need to change the code to get the list of items from a specific category only. So like: select items where category food is 15.
I tried different things, but I'm not so good in php. So could you please help me to change the code. Here is the part where the listing are published.
<div class="col-md-6">
<div class="box_style_2" id="main_menu">
<h2 class="inner">Menu</h2>
<?php
$terms = get_terms( 'foodcat', array(
'include' => $category_food,
));
?>
<?php
$i = 1;
foreach($terms as $term){ ?>
<h3 <?php if($i=1){?>class="nomargin_top"<?php }else{}?> id="<?php echo esc_attr($term->slug);?>"><?php echo esc_attr($term->slug);?></h3>
<p><?php echo esc_attr($term->description);?></p>
<table class="table table-striped cart-list">
<thead>
<tr>
<th>
<?php echo esc_html__( 'Item', 'quickfood' );?>
</th>
<th>
<?php echo esc_html__( 'Price', 'quickfood' );?>
</th>
<th>
<?php echo esc_html__( 'Order', 'quickfood' );?>
</th>
</tr>
</thead>
<tbody>
<?php
$food_arr = new WP_Query(
array(
'post_type' => 'food',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'foodcat',
'field' => 'id',
'terms' => $term->term_id,
)
)
)
);
$ii = 1;
if($food_arr->have_posts()) : while($food_arr->have_posts()) : $food_arr->the_post();
$textmoney = get_post_meta(get_the_id(), '_cmb2_textmoney', true);
?>
<tr id="detail-<?php the_id();?>">
<input type="hidden" value="<?php the_id();?>">
<td>
<h5><?php echo esc_attr($ii);?>.<span class="name-<?php the_id();?> item_name"><?php the_title();?></span>
<input type="hidden" id="name-<?php the_id();?>" value="<?php the_title();?>">
</h5>
<p>
<?php the_content();?>
</p>
</td>
<td>
<strong><?php if(isset($currency_menu) && !empty($currency_menu)){ echo esc_attr($currency_menu);}else{}?><span class="price-<?php the_id();?> price"> <?php echo esc_attr( $textmoney );?></span></strong>
<input type="hidden" id="price-<?php the_id();?>" value="<?php echo esc_attr( $textmoney );?>">
</td>
<td class="options">
<i class="icon_plus_alt2 add-to-cart-button" id="<?php the_id();?>"></i>
</td>
</tr>
<?php $ii++; endwhile;endif;?>
</tbody>
</table>
<hr>
<?php } ?>
</div>
</div>
If I understood you right, all you need is to put the category number in this code:
$food_arr = new WP_Query(
array(
'post_type' => 'food',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'foodcat',
'field' => 'id',
'terms' => $term->term_id,
)
)
)
);
exactly where it says 'terms' => $term->term_id, add the number like this:
'terms' => '15',
Trying to order some posts I'm displaying on a single custom post type page with random, but they aren't random at all. :/
<?php
// Grab the taxonomy term slug for the current post
$terms = get_the_terms( get_the_ID(), 'category-staff' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->slug;
}
$on_draught = join( ", ", $draught_links );
?>
<div class="container hidden-xs">
<div class="row">
<div class="col-sm-12">
<hr />
<h3 class="text-center">Other People At Our Great Resort</h3>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-lg-10 col-lg-offset-1">
<div class="row staff-list">
<?php
// WP_Query arguments
$args2 = array (
'post_type' => 'staff',
'tax_query' => array(
array(
'taxonomy' => 'category-staff',
'field' => 'slug',
'terms' => $on_draught,
),
),
'nopaging' => false,
'posts_per_page' => '4',
'order' => 'DESC',
'orderby' => 'rand',
);
// The Query
$query2 = new WP_Query( $args2 );
// The Loop
if ( $query2->have_posts() ) {
while ( $query2->have_posts() ) {
$query2->the_post(); ?>
<div class="staff staff-other col-sm-3 text-center">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php echo get_the_post_thumbnail( $_post->ID, 'large', array( 'class' => 'img-responsive img-circle img-staff' ) ); ?>
<h4><?php the_title(); ?></h4>
<?php if (get_field('staff_job')) { ?>
<p><?php the_field('staff_job'); ?></p>
<?php } ?>
</a>
</div>
<?php }
} else { ?>
<?php }
// Restore original Post Data
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
<?php endif; // terms if statement ?>
Turns out it was something to do with WPEngine. They disable rand() from the server and it needs to be enabled manually.
Another solution may be to add this code before running the new WP_Query($args) function.
remove_all_filters('posts_orderby');
https://developer.wordpress.org/reference/functions/remove_all_filters/