Search not showing pagination when query is searched - php

I'm building a hotel finder site and want to filter my search results by custom fields when the user searches a location. So for example when a user types in 'London' it will find hotels in London based on some custom fields they have added. But it only works when the search is example.com/?s= if I search a location like example.com/?s=London my pagination doesn't show. But if I add no location and leave the search blank like example.com/?s= pagination shows and works correctly. Would anyone know why? I'm not that great at php so I really need some help. My current search.php file looks like this.
<?php $unique_id = esc_attr( uniqid( 'search-form-' ) ); ?>
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="search" id="<?php echo $unique_id; ?>" class="search-field" value="<?php echo get_search_query(); ?>" name="s" />
<button type="submit" class="search-submit">Search</button>
</form>
<?php $searchm = get_search_query(); ?>
<?php if ( get_search_query() == '' ): ?>
<?php
$args_a = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'CurrentLocation',
'value' => array($areauserwants, areauserwants2),
'compare' => 'IN',
),
array(
'key' => 'AreaLookingFor',
'value' => $areaihave,
'type' => '',
'compare' => '=',
),
array(
'key' => 'NumberOfBeds',
'value' => $howmanybedsdoiwant,
'type' => '',
'compare' => 'IN',
),
array(
'key' => 'TypeOfHotel',
'value' => $hoteltype,
'type' => '',
'compare' => 'IN',
),
),
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
); ?>
<?php $query_a = new WP_Query( $args_a ); ?>
<?php else : ?>
<?php
$args_a = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'CurrentLocation',
'value' => $searchm,
'compare' => '=',
),
array(
'key' => 'AreaLookingFor',
'value' => $areaihave,
'type' => '',
'compare' => '=',
),
),
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
); ?>
<?php $query_a = new WP_Query( $args_a ); ?>
<?php endif; ?>
<?php if ( $query_a->have_posts() ) : ?>
<ul>
<!-- the loop -->
<?php while ( $query_a->have_posts() ) : $query_a->the_post(); ?>
<?php get_template_part( 'template-parts/post/content', 'excerpt' ); ?>
<?php endwhile; ?>
<!-- end of the loop -->
</ul>
<?php next_posts_link( 'Next', $custom_query->max_num_pages );
previous_posts_link( 'Previous' ); ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'No homes matched your criteria' ); ?></p>
<?php endif; ?>

Related

How do I display taxonomy info from a wp-query?

I've got a wp-query that works a treat on a small section of my website's dashboard - it displays a list of repeat jobs (CPTs) that are due in 6 weeks time - I'm having issues with displaying a taxonomy related to the post.
So far I've got
<?php
// get posts
$before_date = date("Ymd", strtotime("+6 weeks"));
$posts = get_posts(array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'pre_job_due_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'pre_job_due_date',
'value' => $before_date,
'compare' => '<',
),
),
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'repeat' )
),
),
));
if( $posts ): ?>
<hr>
<div class="dashpanel">
<div class="duedate-head">Due Date</div>
<div class="jobnumber-head">Job Type</div>
<div class="client-head">Client/Requestor</div>
<div class="customer-head">Customer</div>
</div>
<hr>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<?php $job_type = get_field('pre_job_job_type', $client->ID ); ?>
<?php $customer = get_field('pre_job_customer', $client->ID ); ?>
<?php $job_client = get_field('pre_job_requestor', $client->ID ); ?>
<div class="dashpanel">
<a href="<?php the_permalink(); ?>">
<div class="duedate"><?php the_field('pre_job_due_date'); ?></div>
<div class="jobnumber"><?php echo $job_type[0]->post_title; ?></div>
<div class="client"><?php echo $job_client[0]->post_title; ?></div>
<div class="customer"><?php echo $customer[0]->post_title; ?></div></a>
</div>
<hr>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p>No upcoming jobs to book in.</p>
<?php endif; ?>
I'm not sure where I need to put the
<?php
$pre_job_type = get_field('pre_job_job_type');
if( $term ): ?>
Code - every time I add this code it breaks. Or am I going completely wrong somewhere?
You can add this code after you open the if ($posts) loop:
It will show the taxonomy to which this publication is related. with a link to it.
If you prefer, you can remove the link and display only the title of the term with the call $term->name
<?php
$terms = get_the_terms( $post->ID, 'YOUR_TAXONOMY_HERE' );
if ( $terms != null ) {
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'YOUR_TAXONOMY_HERE' );
echo '<li>' . $term->name . ' ' . $term->term_id . ' ' . $term->count . '</li>';
unset( $term );
}
}
?>
Have you created your expected taxonomy? If haven't at first create the taxonomy on your functions.php file or where you have created your custom post type.
Example:
/**
* Register a private 'Genre' taxonomy for post type 'book'.
*
* #see register_post_type() for registering post types.
*/
function wpdocs_register_taxonomy() {
$args = array(
'label' => __( 'Job Type', 'textdomain' ),
'public' => true,
'rewrite' => true,
'hierarchical' => true
);
register_taxonomy( 'pre_job_job_type', 'pre_jobs', $args );
}
add_action( 'init', 'wpdocs_register_taxonomy', 0 );
Or OOP approach:
public function wpdocs_register_taxonomy() {
$args = array(
'label' => __( 'Job Type', 'textdomain' ),
'public' => true,
'rewrite' => true,
'hierarchical' => true
);
register_taxonomy( 'pre_job_job_type', 'pre_jobs', $args );
}
/**
* When class is instantiated
*/
public function __construct() {
add_action('init', array($this, 'wpdocs_register_taxonomy'));// Register texonomy
}
Now your taxonomies are ready to display. Place this code where ever you want to display the taxonomies.
$pre_job_types = get_categories('taxonomy=pre_job_job_type&post_type=pre_jobs');
foreach ($pre_job_types as $job_type ) : ?>
<span> <?php echo $job_type->name; </span>
endforeach;
If you want to query your custom post with the taxonomy, follow this:
$args = array(
'post_type' => 'pre_jobs',
'status' => 'published',
'tax_query' => array(
array(
'taxonomy' => 'pre_job_job_type',
'field' => 'name',
'terms' => array( 'repeat' )
)
)
);
$query = new WP_Query( $args );
And then continue as a normal query. This query only display the posts whose taxonomy is "repeat".

Including Advanced Custom Fields (ACF) in a Custom Search - Wordpress

I have created / mashed together a cool search through a specified category for my blog. Using Ajax to load the results without the reload.
When I search - no matter the term I search. I receive all posts.
I use ACF for the content & the author. I also reference products using the field featured_product_title. These fields are used within my page like this:
<?php if ( have_rows('case_study_page_content') ): ?>
<?php
while (have_rows('case_study_page_content')): the_row();
$title = get_sub_field('title');
$author = get_sub_field('author');
$content = get_sub_field('content');
?>
<div class="">
<h1 class=""><?php echo $title; ?></h3>
<h3 class=""><?php echo $author; ?></h4>
<p><?php echo $content; ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php
while (have_rows('featured_products')): the_row();
$featured_product_title = get_sub_field('featured_product_title', 'featured_products');
?>
With these in mind my current search looks like this (functions.php):
// CASE STUDY SEARCH
function my_search(){
$args = array(
'orderby' => 'date',
'order' => $_POST['date']
);
if( isset( $_POST['s'] ) ):
/*
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
's' => $_POST['s']
);
*/
if( have_rows('case_study_page_content') ):
while( have_rows('case_study_page_content') ) : the_row();
$title = get_sub_field('title');
$author = get_sub_field('author');
$content = get_sub_field('content');
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => $title,
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
),
array(
'key' => $author,
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
),
array(
'key' => $content,
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
)
)
);
endwhile;
endif;
$query = new WP_Query($args);
if( $query->have_posts() ):
while( $query->have_posts() ):
$query->the_post();
echo "<article class=\"post-box " . get_post_class() . "\">";
echo "";
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
echo "<img src=\"" . $url . "\" />";
echo "<h2>" . get_the_title() . "</h2>";
$case_study = get_field('case_study_page_content');
if( $case_study ):
while( have_rows('case_study_page_content') ): the_row();
$case_study_author = get_sub_field('author');
echo "<p>" . $case_study_author . "</p>";
endwhile;
endif;
echo "</article>";
endwhile;
wp_reset_postdata();
else :
echo 'No case studies found';
endif;
die();
endif;
}
add_action('wp_ajax_customsearch', 'my_search');
add_action('wp_ajax_nopriv_customsearch', 'my_search');
I guess my question is how do I add ACF's into the $args array...?
Please can someone help me successfully compare the 'key' to the 'value' in my WP_Query($args)?
Thanks everyone, Jason.
test this but without conviction
// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'case_study_page_content_title',
'compare' => 'like',
'value' => '%'.$_POST['s'].'',
),
array(
'key' => 'case_study_page_content_author',
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
),
array(
'key' => 'case_study_page_content_content',
'compare' => 'like',
'value' => '%'.$_POST['s'].'%',
)
)
);
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search ) {
$result = $query->query_vars['s'];
$query->query_vars['s'] = '';
$query->set('meta_query', array('relation' => 'OR',
array(
'key' => 'acf_name', // ACF FIELD NAME OR POST META
'value' => $result,
'compare' => 'LIKE',
)
));
$query->set('post_type', 'post'); // optional POST TYPE
}
}
add_filter( 'pre_get_posts', 'custom_search_query');

PHP-code for displaying ACF-fields on Wordpress post?

I have a PHP-code snippet array showing a list of ACF-field values from posts. I would like to use that code on a single post and only show the ACF-values from the current post. So it´s no longer a list. I still need the date filtering.
What do I have to change for it to work like I intend?
<?php
$today = current_time('Ymd');
$args = array(
'post_type' => 'post',
'posts_per_page' => '20',
'meta_key' => 'kalenderdag',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'kalenderdag',
'compare' => '>=',
'value' => $today,
'type' => 'DATE'
),
),
);
$children = new WP_Query($args);
?>
<?php if ($children->have_posts()) : ?>
<?php while ($children->have_posts()) : $children->the_post(); $fields = (object) get_fields(); ?>
<div class="event row">
<div class="event-logo col-sm-4">
<?php the_post_thumbnail( 'large', array( 'class' => 'img-responsive' ) ); ?>
</div>
<div class="event-details col-sm-8">
<h2 class="underline"><a href="<?php the_permalink(); ?>"><?php echo $fields->kalendertitel; ?> - <?php $date = get_field('kalenderdag'); ?>
<?php echo date("d M Y", strtotime($date)); ?></a></h2>
<h3 class="underline"><?php the_title(); ?></h3>
<p><?php echo $fields->kalendertext; ?></p>
<p class="call-to-action">Läs mer</p>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<h2 class="page-title">Just nu har vi inga bokade evenemang</h2>
<p>
Kika gärna in på denna sida en annan gång eller kontakta oss på info#fredenshus.se om du har några frågor.
</p>
<?php endif; ?>
Can you just put the current ID in as an argument with 'p' => get_the_ID() to your WP_Query?
$args = array(
'p' => get_the_ID(),
'post_type' => 'post',
'posts_per_page' => '20',
'meta_key' => 'kalenderdag',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'kalenderdag',
'compare' => '>=',
'value' => $today,
'type' => 'DATE'
),
),
);

Group by a custom value field wordpress posts

I have several posts with a custom field named "series". I want to group all posts by this custom field and below of this i want to list all posts which have not this custom field.
First i wanted to get grouped custom field value and then to be able to query again for all posts with this custom value key and value. But even trying to get the unique custom values does not work.
What i tried is this:
<?php
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_key = "series"';
}
?>
<?php add_filter('posts_groupby', 'query_group_by_filter'); ?>
<?php $states = new WP_Query(array(
'meta_key' => 'series',
'ignore_sticky_posts' => 1
));
?>
<?php remove_filter('posts_groupby', 'query_group_by_filter'); ?>
<ul>
<?php
while ( $states->have_posts() ) : $states->the_post();
$mykey_values = get_post_custom_values( 'series' );
foreach ( $mykey_values as $key => $value ) {
echo "<li>$key => $value ( 'series' )</li>";
}
endwhile;
?>
</ul>
Whats wrong with this code?
WP_Meta_Query
All posts with custom value:
<?php
$args = array(
'post_type' => 'my-post-type',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'series',
'value' => 'my-val',
'compare' => '='
),
array(
'key' => 'series',
'value' => '',
'compare' => '!='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
And without values:
<?php
$args = array(
'post_type' => 'my-post-type',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'series',
'value' => '',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata(); ?>
</ul>
<?php endif; ?>

exclude a custom taxonomy from wp_query

IN my wp site, I have 2 category and a few post like that..
cat_1- post 1, post 2, post 3.
cat_2- post 2, post 3, post 4.
When i am in post 3 page, i want to show releted article only from category 2.here is my code:but it returns empty. Probably i did not catch the logic. Any help would be highly appreciated.
<?php
$terms = get_the_terms( $post_id, 'category' );
if( empty( $terms ) ) $terms = array();
$term_list = wp_list_pluck( $terms, 'slug' );
$related_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'post__not_in' => array( get_the_ID() ),
'orderby' => 'desc',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term_list
),
array(
'taxonomy' => 'category',
'terms' => array('cat_1'),
'field' => 'slug',
'operator' => 'NOT IN',
),
),
);
$related = new WP_Query( $related_args );
if( $related->have_posts() ):
?>
<div class="post-navigation">
<h3>Related posts</h3>
<ul>
<?php while( $related->have_posts() ): $related->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
</div>
<?php
endif;
wp_reset_postdata();
?>
here is the following query ...may be helpful to you...
$custom_tex=array(array('taxonomy'=>'category','field'=>'slug','terms'=>'cat_2'));
$new = new WP_Query(array('post_type'=>'post','posts_per_page'=>-1, 'tax_query'=>$custom_tex,'order' => 'DESC','orderby' => 'ID' ));
while ($new->have_posts()) : $new->the_post();
echo $post->post_title;
echo "<br>";
endwhile;

Categories