Custom taxonomy archive showing all posts - php

I have a custom post type 'vacatures' with two custom taxonomies 'type' and 'locatie'. I created an archive page to display all the posts, but now when I view the link for all the posts belonging to a specific 'type', it just shows all the posts like the archive page.
How can I solve this, making sure I can still make the filters work?
archive-vacatures.php:
<?php get_header(); ?>
<div class="jobs__banner">
<h1><?php _e( 'VACATURES', 'ago' ) ?></h1>
</div>
<div class="vacatures__archief">
<?php
$types = get_terms([ 'taxonomy' => 'type' ]);
$locations = get_terms([ 'taxonomy' => 'locatie']);
$postsQuery = [
'order' => 'DESC',
'post_type' => 'vacatures',
'posts_per_page' => -1,
'tax_query' => []
];
if(array_key_exists('type_id', $_GET) && $_GET['type_id']) {
$postsQuery['tax_query'][] = [
'taxonomy' => 'type',
'field' => 'term_id',
'terms' => [$_GET['type_id']]
];
}
if(array_key_exists('locatie_id', $_GET) && $_GET['locatie_id']) {
$postsQuery['tax_query'][] = [
'taxonomy' => 'locatie',
'field' => 'term_id',
'terms' => [$_GET['locatie_id']]
];
}
if(array_key_exists('query', $_GET) && $_GET['query']) {
$postsQuery['s'] = $_GET['query'];
}
query_posts($postsQuery); ?>
<form class="vacature__filter">
<input type="text" name="query" placeholder="Search.." />
<select name="type_id">
<option value="">-- <?php _e( 'Alle types', 'ago' ) ?> --</option>
<?php foreach($types as $type) {
$selected = array_key_exists('type_id', $_GET) && $_GET['type_id'] == $type->term_id ? 'selected' : '';
echo "<option value='{$type->term_id}' {$selected}>{$type->name}</option>";
} ?>
</select>
<select name="locatie_id">
<option value="">-- <?php _e( 'Alle locaties', 'ago' ) ?> --</option>
<?php foreach($locations as $locatie) {
$selected = array_key_exists('locatie_id', $_GET) && $_GET['locatie_id'] == $locatie->term_id ? 'selected' : '';
echo "<option value='{$locatie->term_id}' {$selected}>{$locatie->name}</option>";
} ?>
</select>
<button type="submit">Zoek</button>
</form>
<?php if ( have_posts() ) { ?>
<?php
while ( have_posts() ) {
the_post();
?>
<article id="vacature-<?php the_ID(); ?>" <?php post_class(); ?>>
<a class="vacature__item" href="<?php the_permalink(); ?>">
<h2 class="vacature__title"><?php the_title(); ?></h2>
<h4 class="vacature__company"><?php echo get_field('vacature_bedrijf') ?> - <?php $terms = get_the_terms( $post->ID , 'type' ); $i = 1; foreach ( $terms as $term ) { $term_link = get_term_link( $term, 'type' ); if( is_wp_error( $term_link ) ) continue; echo $term->name; echo ($i < count($terms))? ", " : ""; $i++; } ?></h4>
<h6 class="vacature__location"><?php $terms = get_the_terms( $post->ID , 'locatie' ); foreach ( $terms as $term ) { echo $term->name; } ?></h6>
</a>
</article>
<?php
}
}
?>

Related

Post filtering in wordpress returns all posts

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 :)

How get terms of specific taxonomy in wordpress?

I am working on properties project and need to get terms of specific taxonomy. I mean properties of cities base of their states.
Here is the code I have written for:
<div class="row">
<div class="col-md-2">
<?php
$taxonomy = 'property-state';
$terms = get_terms($taxonomy);
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li>
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<?php echo $term->name . ' ('. $term->count .')' ?>
</a>
</li>
<?php } ?>
</ul>
<?php
?>
</div>
<div class="col-md-2">
<?php
$taxonomy = 'property-city';
$terms = get_terms($taxonomy);
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li>
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<?php echo $term->name . ' ('. $term->count .')'?>
</a>
</li>
<?php } ?>
</ul>
<?php
?>
</div>
</div> <!-- end of row -->
What I need is to list properties of cities base of state, how should I change these codes?
I found my answer myself, I wanted to list properties of specific state with specific status.
for example the properties of Kabul state with status of (for-rent) and the properties count on each city of that state.
<?php $terms = get_terms("property-city");
foreach($terms as $term)
{
$items = get_posts(array(
'numberposts' => -1,
'post_type' => 'property',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'property-city',
'field' => 'slug',
'terms' => $term->slug
),
array(
'taxonomy' => 'property-state',
'field' => 'name',
'terms' => 'هرات',
'operator' => 'NOT IN'
),
)
));
$count = count( $items );
?>
<ul>
<li>
<a href="<?php echo get_term_link($term);?>" <?php if ($count == 0) echo " style='display: none';"; ?>>
<?php echo $term->name. ' ('. $count .')';?>
</a>
</li>
</ul>
<?php
}
?>

URL variables not return results

I use custom WP Query on several areas on my site and with the recent WordPress 4.9.1 update my WP Query sections have ceased functioning. I know there is a way around this issue that does not involve rolling back to WordPress 4.8, but I'm not personally familiar enough with php or WP Query to know how fix it.
The problem I'm having is not when the page loads initially, but when the query variables of location, category, and alphabet are used. Any time a query is run using these variables it returns no results.
Alternatively running a keyword query or max posts query functions perfectly.
I'd appreciate help from anyone who can locate the problem in my code below.
Here's the code that I'm using with WP Query:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$category = get_query_var( 'nabc', false );
$location = get_query_var( 'nabl', false );
$keywords = get_query_var( 'nabk', false );
$alphabet = get_query_var( 'naba', false );
$maxposts = get_query_var( 'showall', 10 );
$meta_query = array(
'relation' => 'AND',
);
if( $category && strtoupper( $category ) != "AFFILIATE OR REGION" ) {
// Category / Type search
$meta_query[] = array(
'key' => 'directory_fields_%_region_or_affiliate',
'value' => $category,
'compare' => '='
);
}
if( $location && strtoupper( $location ) != "STATE/PROVINCE" ) {
// State/Location
$meta_query[] = array(
'key' => 'directory_fields_%_state',
'value' => $location,
'compare' => '='
);
}
if( $keywords ) {
// keyword search
$meta_query[] = array(
'value' => $keywords,
'compare' => 'LIKE'
);
// d( $meta_query );
}
if( $alphabet ) {
// Search by Letter
$meta_query[] = array(
'key' => 'directory_fields_%_entity_name',
'value' => $alphabet,
'compare' => 'BETWEEN'
);
}
// Get all the categories
$categories = array();
global $wpdb;
$list = $wpdb->get_results( 'SELECT meta_value FROM ' . $wpdb-
>postmeta . ' WHERE meta_key LIKE
"directory_fields_%_region_or_affiliate" GROUP BY meta_value' );
foreach( $list as $l )
{
if( $l->meta_value )
$categories[] = $l->meta_value;
}
// Get all the States
$states = array();
$list = $wpdb->get_results( 'SELECT meta_value FROM ' . $wpdb-
>postmeta . ' WHERE meta_key LIKE "directory_fields_%_state" GROUP BY
meta_value' );
foreach( $list as $l )
{
if( $l->meta_value )
$states[] = $l->meta_value;
}
?>
<?php get_header(); ?>
<?php $heroImage = get_field( 'hero_image' ); ?>
<div class="jumbotron" style="background: url( <?php echo
!empty($heroImage['url']) ? $heroImage['url'] : get_field(
'nab_directory_hero_image', 'options' ); ?> ) center center no-
repeat;background-size: cover;" class="hero-image">
<h1>Directory</h1>
</div>
<!-- START FRONT CONTENT -->
<div id="content" style="position:relative;">
<div class="row mobile-padding">
<div class="container jobs-container">
<div class="col-xs-24">
<div class="filters container-fluid hidden-print">
<form class="form-inline row" action="/directory/" method="GET">
<div class="form-group col-sm-6">
<input class="form-control" type="text" name="nabk" value="<?php echo esc_html( $keywords ); ?>" placeholder="KEYWORDS">
</div>
<div class="form-group col-sm-6 col-sm-offset-1">
<select class="form-control" name="nabc">
<option>AFFILIATE OR REGION</option>
<?php foreach( $categories as $cat ): ?>
<option <?php echo $category == $cat ? 'selected' : '' ?>><?= $cat ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group col-sm-6 col-sm-offset-1">
<select class="form-control" name="nabl">
<option>STATE/PROVINCE</option>
<?php foreach( $states as $state ): ?>
<option <?php echo $location == $state ? 'selected' : '' ?>><?= $state ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group col-sm-4 text-right">
<button class="nab-btn nab-btn-blue job-filter-btn" type="submit">SEARCH</button>
</div>
</form>
</div>
<div class="alphabet-filters hidden-print">
<ul>
<?php for( $i = 'A'; $i != 'AA'; $i++ ): $j = $i ?>
<li><?= $i ?></li>
<?php endfor; ?>
</ul>
</div>
<?php
global $wp_query;
$wp_query = new WP_Query( array(
'post_type' => 'directory',
'posts_per_page' => $maxposts,
'paged' => $paged, // 104 current last page
'meta_query' => $meta_query,
'orderby' => 'title',
'order' => 'ASC'
) );
// query_posts( array(
// 'post_type' => 'directory',
// 'posts_per_page' => $maxposts,
// 'paged' => $paged,
// 'meta_query' => $meta_query,
// 'orderby' => 'title',
// 'order' => 'ASC'
// ) );
?>
<?php if( $wp_query->have_posts() ): ?>
<div class="table-responsive">
<table id="directory-listing" class="table">
<thead>
<tr>
<th>CHURCH/ORGANIZATION</th>
<th>STATE/PROVINCE</th>
<th>NAME</th>
</tr>
</thead>
<tbody>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); global $post; ?>
<?php if( have_rows( 'directory_fields', $post->id ) ): ?>
<?php while ( have_rows( 'directory_fields', $post->id ) ) : the_row(); ?>
<?php if( get_row_layout() == 'directory_details' ): ?>
<tr>
<th scope="row"><?= get_sub_field( 'entity_name' ); ?><?= get_sub_field( 'street_address_1' ) ?><p><?= get_sub_field('street_address_2') ?></p><p><?= trim(get_sub_field( 'city' )) ?></p><p><?= get_sub_field( 'business_phone' ); ?></p></th>
<td><i class="fa fa-map-marker" aria-hidden="true"> </i><?= get_sub_field( 'state' ); ?></td>
<td><?= get_sub_field( 'person_name' ); ?><p><?= get_sub_field( 'person_title' ); ?></p></td>
</tr>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php endwhile;?>
</tbody>
</table>
</div> <!-- /.table-responsive -->
</div>
<nav class="page-navigation hidden-print" aria-label="Page navigation">
<?php nab_bootstrap_pagination(); ?>
</nav>
<div class="directory-navbar hidden-print">
<a href="?showall=-1&nabc=<?= $category ?>&nabk=<?= $keywords ?>&nabl=<?= $location ?>">
<button class="nab-btn nab-btn-blue">Show All</button>
</a>
<a href="javascript:window.print()">
<button class="nab-btn nab-btn-blue">Print</button>
</a>
</div>
<?php else: ?>
<p class="text-center">No Results Found</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div> <!-- /.jobs-container -->
</div><!-- end .row -->
</div><!-- end #content -->
Sorry for the full page of code, but I honestly don't know where the problem is in this code.
For anyone else with a similar problem, the fix is actually simple on this one.
For some reason WordPress 4.9.1 changed the SQL database to use the "0" character instead of the "%" character. This means that any query that used "%" must now use "0".
For me this meant switching from:
if( $category && strtoupper( $category ) != "AFFILIATE OR REGION" ) {
// Category / Type search
$meta_query[] = array(
'key' => 'directory_fields_%_region_or_affiliate',
'value' => $category,
'compare' => '='
);
} .
to:
if( $category && strtoupper( $category ) != "AFFILIATE OR REGION" ) {
// Category / Type search
$meta_query[] = array(
'key' => 'directory_fields_0_region_or_affiliate',
'value' => $category,
'compare' => '='
);
} .
Hope this is helpful to someone, although your mileage may vary.

Page 2 Resets POST filter

i have following page where i filter posts based on their category. This filter works fine, but if there are multiple pages of filtered results and i click to go to page 2 the filter resets and shows every post again instead of page 2 of the filtered results. How can i go to the second page of the filtered results?
Code:
<form class="form-inline" method="POST" action="/intranet/bibliotheek/">
<?php $intranetCategorie = get_terms( 'intranet_categorie');
if (!empty($intranetCategorie) && !is_wp_error($intranetCategorie )) {
echo '<select class="form-control training-drop" name="intranetCategorie">';
echo '<option value="empty">Alle Items</option>';
foreach ($intranetCategorie as $terms) {
if(0 != $terms->parent ){
echo '<option value="'. $terms->slug .'">' .$terms->name.'</option>';
}
}
echo '</select>';
}
?>
<input class="btn btn-intra" type="submit" value="Filter Resultaten">
</form>
<?php foreach ($intranetCategorie as $terms) {
$categorie[] = $terms->slug;
}
if(isset($_POST['intranetCategorie']) && $_POST['intranetCategorie'] != 'empty'):
$categorie = $_POST['intranetCategorie'];
endif;
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'intranet',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'intranet_categorie',
'field' => 'slug',
'terms' => 'bibliotheek',
),
array(
'taxonomy' => 'intranet_categorie',
'field' => 'slug',
'terms' => $categorie,
),
),
'posts_per_page' => 5,
'paged' => $paged
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3 class="intranet-title"><?php the_title(); ?></h3>
<p class="post-info">Geplaatst op <?php the_time('j F Y'); ?></p>
<p><?php the_content(); ?></p>
<?php
$file = get_field('upload');
if (!empty($file)) {
if( $file ):
// vars
$url = $file['url'];
$title = $file['title'];
$caption = $file['caption'];
// icon
$icon = $file['icon'];
if( $file['type'] == 'image' ) {
$icon = $file['sizes']['thumbnail'];
}
if( $caption ): ?>
<div class="wp-caption">
<?php endif; ?>
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank">
<img src="<?php echo $icon; ?>" />
<span><?php echo $title; ?></span>
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
<?php } ?>
<?php if ( 'post' == get_post_type() ) : ?>
<footer class="edit">
<?php edit_post_link( __( 'Bewerk', 'soml' ), '<span class="glyphicon glyphicon-pencil"> ', '</span>' ); ?>
</footer><!-- .entry-footer -->
<?php else : ?>
<?php edit_post_link( __( 'Bewerk', 'soml' ), '<footer class="entry-footer"><span class="glyphicon glyphicon-pencil"> ', '</span></footer><!-- .entry-footer -->' ); ?>
<?php endif; ?>
<hr>
<?php endwhile; ?>
<div class="pagination-links-intranet">
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages,
'prev_text' => '« Vorige',
'next_text' => 'Volgende »'
) );
?>
</div>
<?php else : ?>
<?php echo'<p>Er zijn geen berichten gevonden</p>' ?>
<?php endif; ?>
Pagination after i filter on a category (working as intended):
Pagination after i select page 2 of the filtered results:
The filter resets and will show all posts again. It should go to the second page of the filtered results.
Switching $_POST to $_GET will fix the problem since $_POST doesn't carry over when switching pages.

How to list subcategories of a category in categories page template (wordpress)

I have a categories page template, listing all categories with featured images. But I want to show only subcategories of a parent category. I don't know where to modify the template. Here is the code.
get_header(); ?>
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<h1 class="border-radius-5"><?php the_title(); ?></h1>
<div id="page" class="post-content">
<?php the_content(); ?>
<?php
$terms = get_terms("category", $args);
$count = count($terms);
$categories = array();
if ($count > 0) {
echo '<ul class="listing-cat">';
foreach ($terms as $term) {
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$video_from_categorie = new WP_Query( $args );
if( $video_from_categorie->have_posts() ){
$video_from_categorie->the_post();
}else{}
$term->slug;
$term->name;
?>
<li class="border-radius-5 box-shadow">
<?php echo get_post_image();?>
<span><?php echo $term->name; ?></span>
<span class="nb_cat border-radius-5"><?php echo $term->count; ?> <?php if ( $term->count > 1 ){
_e( 'videos', get_theme_name() );
}else{
_e( 'video', get_theme_name() );
} ?></span>
</li>
<?php
}
echo '</ul>';
echo '<div class="clear"></div>';
}
?>
</div><!-- #page -->
<?php endwhile; ?>
<?php endif; ?>
Pass the ID of the desired parent term/category to the child_of parameter of get_terms():
$terms = get_terms( 'category', array( 'child_of' => TERM_ID_GOES_HERE ) );

Categories