Page 2 Resets POST filter - php

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.

Related

get specific custom type post in specific category - wordpress

I have a problem, i have a custom type post (authors) and taxonomy (manager and team) called position
I need to get all authors of the team on a page and need the manager to be at the first one of them
the authors ordered by name but the manager name begin with "T"
what can I do with this situation.
my code is
<?php
/**
* The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
* Template Name: taxonomies
*
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
get_header(); ?>
<?php
// echo the $slug name of the page
$page_names = get_the_title(); ?>
<section id="articles" class="articles">
<div class="<?php echo $classcase ?>">
<?php
$loop = new WP_Query( array( 'post_type'=>'authors','posts_per_page' => '-1','orderby' => 'title','order'=>'ASC' ) );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</section>
<div class='endauthors'></div>
For this task you need to use tax_query https://developer.wordpress.org/reference/classes/wp_tax_query/
First we take all managers sorted by title, then the entire team
Try this code
<?php
/**
* The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
* Template Name: taxonomies
*
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
get_header(); ?>
<?php
// echo the $slug name of the page
$page_names = get_the_title(); ?>
<section id="articles" class="articles">
<div class="<?php echo $classcase ?>">
<?php
$args = array(
'post_type' => 'authors',
'posts_per_page' => '-1',
'orderby' => 'title',
'order'=>'ASC'
'tax_query' => array(
array(
'taxonomy' => 'position',
'field' => 'slug',
'terms' => array( 'manager' )
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
<?php
$args = array(
'post_type' => 'authors',
'posts_per_page' => '-1',
'orderby' => 'title',
'order'=>'ASC'
'tax_query' => array(
array(
'taxonomy' => 'position',
'field' => 'slug',
'terms' => array( 'team' )
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</section>
<div class='endauthors'></div>

Adding pagination to wordpress shortcode

Following advice on Wordpress Pagination in a Shortcode I'm working on a shortcode for wordpress and trying to add pagination to it. I'm almost there, just don't know why wordpress isn't updating paginated pages content, e.g. if I click on "Next" or "1", "2", "3" etc I always see the same page.
This is my code:
function carforyou_LatestCar($atts){
ob_start();?>
<div class="row">
<?php
extract( shortcode_atts(array('show' =>''), $atts ));
extract( shortcode_atts(array('brand' =>''), $atts ));
$loop = new WP_Query( array(
'post_type' => 'auto',
'auto-brand' => $brand,
'posts_per_page'=>$show,
'paged' => $paged,
'offset' => 0
));
while ($loop->have_posts()) : $loop->the_post();
global $paged; ?>
<div class="col-list-3">
<div class="featured-car-list">
<div class="featured-car-img">
<a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
<?php if(has_post_thumbnail()):
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif;
?>
</a>
<?php carforyou_AutoType(); ?>
<div class="compare_item">
<div class="checkbox">
<button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
</div>
</div>
</div>
<div class="featured-car-content">
<h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
<div class="price_info">
<?php if(!empty($post->DREAM_auto_price)): ?>
<p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
<?php endif; ?>
<div class="car-location">
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single)
$location = $term_single->name;
?>
<?php if(!empty($location)): ?>
<span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
<?php endif; ?>
</div>
</div>
<ul>
<?php carforyou_featuredList(); ?>
</ul>
</div>
</div>
</div>
<?php endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
wp_reset_query();
?>
</div>
<?php }
Any advice? :)
Thank you!
add in functions.php
// Numbered Pagination
if ( !function_exists( 'kris_pagination' ) ) {
function kris_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$total = $wp_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
shortcode
<?php kris_pagination(); ?>

Wordpress static page template pagination not working

I'm building a custom page template and I can't seem to get the pagination to work. It keeps showing the same posts as on the home/first page. I have tried a lot of different code but getting the same problem with each results.
This is the current query I am using:
<?php
global $paged;
global $wp_query;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=2&post_type=post'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="news-item">
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
?>
<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
</div>
<?php endwhile; ?>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php
$wp_query = null;
$wp_query = $temp;
?>
Found the solution, this code worked:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => '2',
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="news-item">
<?php
if ( has_post_thumbnail() ) {
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
echo '<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>';
}
else {
}
;?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
<div id="single-post-container"></div>
<a class="button-1 load-more" href="<?php echo get_permalink(); ?>">Lees meer</a>
<a class="ajax-close button-1" href="#">X</a>
</div>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Try this code just copy pase
<?php
$args = array(
'post_type'=> 'post',
//'category_name' => 'blog',
'orderby' => 'post_date',
//'posts_per_page'=>'1'
'paged' => get_query_var('paged')
);
query_posts( $args );
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php // Wordpress Pagination
$big = 999999999; // need an unlikely integer
$links = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '<',
'next_text' => '>',
'type' => 'array'
) );
if(!empty($links)){ ?>
<ul class="pagination">
<?php
foreach($links as $link){
?>
<li><?php echo $link; ?></li>
<?php
}
wp_reset_query(); ?>
</ul>
<?php } ?>

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

Thumbnail Gallery: Displaying only Main Categories, and not the Sub-Categories

I am using the eList wordpress theme.
Now on the main page, there is a thumbnail display of all the categories listed. Now, on the theme you will see that they have no sub-categories under the categories, only listings
Now in my theme, I have sub-categories, and the theme does make provision for that. See my site here and the dropdown list next to the search bar in the header to see what I mean about the subcategories
Question:
How do I only list the MAIN categories on the home page, and NOT the sub-categories of the main categories as well?
Here is the code of the home page part that controls the thumbnails:
<?php
$elist_categories_args = array( 'hide_empty' => 0 );
if ( 'on' == get_option('elist_listings_hide_empty') ) $elist_categories_args['hide_empty'] = 1;
if ( is_tax() ) {
$et_term = get_queried_object();
$elist_categories_args['child_of'] = $et_term->term_id;
}
$categories = get_categories( 'taxonomy=listing-category' );
$elist_listing_categories = get_terms( 'listing_category', apply_filters( 'listing_categories_args', $elist_categories_args ) );
$elist_category_images = false !== get_option( 'elist_category_images' ) ? (array) get_option( 'elist_category_images' ) : array();
$et_count = 0;
if ( $elist_listing_categories ){ ?>
<section id="listing-categories">
<div class="container clearfix">
<h1><?php esc_html_e( 'Listing Categories', 'eList' ); ?></h1>
<?php foreach( $elist_listing_categories as $elist_listing_category ) { ?>
<?php
$et_current_term_query = new WP_Query(
array(
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'id',
'terms' => $elist_listing_category->term_id
)
)
)
);
?>
<?php $et_count++; ?>
<div class="l-category<?php if ( $et_count % 3 == 0 ) echo ' last'; ?>">
<?php $et_listing_category_link = get_term_link( $elist_listing_category ); ?>
<?php if ( isset( $elist_category_images[$elist_listing_category->term_id] ) && '' != $elist_category_images[$elist_listing_category->term_id] ) { ?>
<div class="thumb">
<a href="<?php echo esc_url( $et_listing_category_link ); ?>">
<img class="item-image" alt="<?php echo esc_attr( $elist_listing_category->name ); ?>" src="<?php echo esc_attr( et_new_thumb_resize( et_multisite_thumbnail( $elist_category_images[$elist_listing_category->term_id] ), 70, 70, '', true ) ); ?>"/>
<span class="overlay"></span>
</a>
</div> <!-- end .thumb -->
<?php } ?>
<div class="description">
<h2><?php echo esc_html( $elist_listing_category->name ); ?></h2>
<p class="info"><?php if ( 1 == $et_current_term_query->found_posts ) printf( __('%d Listing','eList'), $et_current_term_query->found_posts ); else printf( __('%d Listings'), $et_current_term_query->found_posts ); ?></p>
<?php if ( '' != $elist_listing_category->description ) { ?>
<p><?php echo esc_html( $elist_listing_category->description ); ?></p>
<?php } ?>
</div> <!-- end .description -->
</div> <!-- end .l-category -->
<?php } ?>
</div> <!-- end .container -->
</section> <!-- end #listing-categories -->
<?php } ?>
To pinpoint out of the above code, here is the html part that holds the dynamically populated thumbnail:
<div class="l-category<?php if ( $et_count % 3 == 0 ) echo ' last'; ?>">
<?php $et_listing_category_link = get_term_link( $elist_listing_category ); ?>
<?php if ( isset( $elist_category_images[$elist_listing_category->term_id] ) && '' != $elist_category_images[$elist_listing_category->term_id] ) { ?>
<div class="thumb">
<a href="<?php echo esc_url( $et_listing_category_link ); ?>">
<img class="item-image" alt="<?php echo esc_attr( $elist_listing_category->name ); ?>" src="<?php echo esc_attr( et_new_thumb_resize( et_multisite_thumbnail( $elist_category_images[$elist_listing_category->term_id] ), 70, 70, '', true ) ); ?>"/>
<span class="overlay"></span>
</a>
</div> <!-- end .thumb -->
<?php } ?>
<div class="description">
<h2><?php echo esc_html( $elist_listing_category->name ); ?></h2>
<p class="info"><?php if ( 1 == $et_current_term_query->found_posts ) printf( __('%d Listing','eList'), $et_current_term_query->found_posts ); else printf( __('%d Listings'), $et_current_term_query->found_posts ); ?></p>
<?php if ( '' != $elist_listing_category->description ) { ?>
<p><?php echo esc_html( $elist_listing_category->description ); ?></p>
<?php } ?>
</div> <!-- end .description -->
</div> <!-- end .l-category -->
Given the code presented, there seem to be two possibilities.
1) Modify WP_Query arguments
$et_current_term_query = new WP_Query(
array(
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'listing_category',
'field' => 'id',
'terms' => $elist_listing_category->term_id,
'include_children' => false // <--- ADD THIS
)
)
)
);
2) Modify the listing terms call used with get_terms
$elist_categories_args = array( 'hide_empty' => 0, 'parent' => 0 ); // ADD parent

Categories