URL variables not return results - php

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.

Related

WordPress show related posts based on taxonomy term

Below I have this code in single-courses.php which is currently displaying all the the courses. The goal is to show only related courses based on that courses' taxonomy term (category). Help please.
<?php
$related = get_posts( array(
'taxonomy' => 'course_category',
'post_type' => 'courses',
'numberposts' => -1
)
);
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<div class="row next-chapter-list" onclick="location.href='<?php the_permalink();?>';">
<div class="col-md-10 col-9 valign text-left">
<?php the_title(); ?>
</div>
<div class="col-md-2 col-3 valign text-right">
<i class="bi bi-play-fill"></i>
</div>
</div>
<?php }
wp_reset_postdata(); ?>
i found a solution literally after i posted this question, sorry- but this is what worked for me.
<?php
//get the post's venues
$custom_terms = wp_get_post_terms($post->ID, 'course_category');
if( $custom_terms ){
// going to hold our tax_query params
$tax_query = array();
// add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
if( count( $custom_terms > 1 ) )
$tax_query['relation'] = 'OR' ;
// loop through venus and build a tax query
foreach( $custom_terms as $custom_term ) {
$tax_query[] = array(
'taxonomy' => 'course_category',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// put all the WP_Query args together
$args = array( 'post_type' => 'courses',
'posts_per_page' => 5,
'tax_query' => $tax_query );
// finally run the query
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="row next-chapter-list" onclick="location.href='<?php the_permalink();?>';">
<div class="col-md-10 col-9 valign text-left">
<?php the_title(); ?>
</div>
<div class="col-md-2 col-3 valign text-right">
<i class="bi bi-play-fill"></i>
</div>
</div>
<?php
endwhile;
}
wp_reset_query();
}?>

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

Custom taxonomy archive showing all posts

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
}
}
?>

WP_Query orderby 'rand' not working

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/

How to display custom post type by custom taxonomy?

My homepage is displaing my listings( my custom post type) by the order i enter them. I would like my listings that have the custom taxonomy "tag" (Special offer) to be displayed on my first page from my homepage and after that the rest of the listings exactly how they where before. I am new in to wordpress and hope i asked my question right
This is my homepage code
<div id="content">
<?php include (TEMPLATEPATH . '/lib/slider.php'); ?>
<?php
$args = array(
'post_type' => 'listings',
'paged' => $paged,
'showposts' => 8 ,
'oferta' =>"oferta"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="post propbox <?php if (++$counter % 2 == 0) { echo "lastbox"; }?> clearfix" id="post-<?php the_ID(); ?>">
<div class="archimg">
<?php if( has_term( 'featured', 'type', $post->ID ) ) { ?>
<span class="featspan">Featured</span>
<?php } else if ( has_term( 'sold', 'type', $post->ID ) ){ ?>
<span class="soldspan">Sold</span>
<?php } else if ( has_term( 'reduced', 'type', $post->ID ) ){ ?>
<span class="redspan">Reduced</span>
<?php } ?>
<?php
if ( has_post_thumbnail() ) { ?>
<img class="propimg" src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php get_image_url(); ?>&h=180&w=310&zc=1" alt=""/>
<?php } else { ?>
<img class="propimg" src="<?php bloginfo('template_directory'); ?>/images/dummy.jpg" alt="" />
<?php } ?>
</div>
<div class="cover">
<div class="title">
<h2><?php the_title(); ?></h2>
</div>
<div class="propmeta">
<div class="proplist"><span>Price</span> <span class="propval"> <?php $price=get_post_meta($post->ID, 'wtf_price', true); echo $price; ?></span></div>
<div class="proplist"><span>Location</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'location', '', ' ', '' ); ?></span></div>
<div class="proplist"><span>Property type</span> <span class="propval"><?php echo get_the_term_list( $post->ID, 'property', '', ' ', '' ); ?></span></div>
<div class="proplist"><span>Area</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'area', '', ' ', '' ); ?></span></div>
</div>
<div class="entry">
<?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
<a class="morer" href="<?php the_permalink() ?>">Check this</a>
<div class="clear"></div>
</div>
</div>
</div>
<?php endwhile; ?>
<div class="clear"></div>
<?php getpagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>
</div>
This is my main content content sorry how can i rearenge this so it fits my needs
you can use tax_query like by here
$args = get_posts( array(
'post_type' => 'my_post_type',
'tax_query' => array(
array(
'taxonomy' => 'my_taxonomy',
'field' => 'slug',
'terms' => 'webdesign'
)
)
) );
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
You have to pass the arguments array to you $wp_query->query($args); in which you define the tag name or category name , taxonomy of tag or taxonomy of category
$args = array(
'post_type' => 'your custom post type name',
'paged' => $paged,
'showposts' => 8 ,
'your custom goes here taxonomy' =>"tag name or category name"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
Generally taxonomy name is what you defined in register_taxonomy('here_goes_taxonomy',array('post_type'), array(...))
All the answers above are great, but you don't necessarily need to pass the tax_query array. Once you have created your custom taxonomy the right way, you can do the following.
<?php
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$loop = new WP_Query([
'post_type' => 'your_custom_post_type',
'post_status' => 'publish', // fetch only published posts
'posts_per_page' => get_option('posts_per_page') ?: 10, // number of posts to fetch
'paged' => $paged
'your_custom_taxonomy' => 'your_custom_taxonomy_slug_or_name'
]);
// WordPress pagination doesn't work on custom query
// Let's make it work
$temp_query = $wp_query; // store $wp_query in a temporary variable
$wp_query = null; // set it to null
$wp_query = $loop; // store your custom query to $wp_query
if ( $loop->have_posts() ) { // Let's check if we have some posts
while( $loop->have_posts() ){
$loop->the_post();
// add your markup here
}
}
wp_reset_postdata();
$wp_query = null;
$wp_query = $temp_query; // store back the original $wp_query
<?php $cat_terms = get_terms(
array('mobile_category'),
array(
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
'number' => 2 ) );
if( $cat_terms ) : ?>
<?php foreach( $cat_terms as $term ) :
$args = array(
'post_type'=> 'mobile',
'posts_per_page'=> 2,
'post_status'=> 'publish',
'tax_query'=> array(
array(
'taxonomy' => 'mobile_category',
'field' => 'slug',
'terms' => $term->slug,), ),
'ignore_sticky_posts' => true
);
$_posts = new WP_Query( $args );
if( $_posts->have_posts() ) :
while( $_posts->have_posts() ) : $_posts->the_post(); ?>
<span class="color2"><?php echo $term->name ; ?></span>
<?php endwhile;
endif;
wp_reset_postdata();
endforeach; ?>
<?php endif; ?>

Categories