Dynamic meta_query depending on query var paramters in url Wordpress custom post type - wordpress

In my custom post type i want to filter depending on the parameters in the url. For example ?car=yellow&foo=1 refer to meta query key and value. The key and value's are the extra fields. What makes it extra difficult is that
This is what my loop looks like right now:
$args = array(
'post_type'=>'glas', // Your post type name
'posts_per_page' => 9,
'paged' => $paged,
);
$loop = new WP_Query( $args ); ?>
<?php if($loop->have_posts()): ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title(); ?> <?php echo get_field('type_glas'); ?> <?php echo get_field('uitvoering_glas'); ?><br>
<?php endwhile; ?>
<?php $total_pages = $loop->max_num_pages; ?>
<?php if ($total_pages > 1) : ?>
<?php
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
?>
<?php endif; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

Related

How to add pagination to custom page

I am using the loop as you can see in my code. Only 2 posts must be shown and for the rest I should be able to have pagination.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(
array (
'posts_per_page' => 2,
'post_type' => 'post',
'category_name' => 'news',
'category' => 1,
'paged' => $paged )
);
// The Loop
while ( have_posts() ) : the_post();?>
<div class="news-page-content-wrapper">
<div class="news-page-content">
<h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
<figure><?php the_post_thumbnail(); ?></figure>
<p><?php echo get_the_excerpt();?></p>
Read More&raquo
</div>
</div>
<?endwhile;
// Reset Query
wp_reset_query();
?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
How can I have pagination using the loop with category ID?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$data= new WP_Query(array(
'post_type'=>'YOUR_POST_TYPE', // your post type name
'posts_per_page' => 3, // post per page
'paged' => $paged,
));
if($data->have_posts()) :
while($data->have_posts()) : $data->the_post();
// Your code
endwhile;
$total_pages = $data->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
<?php else :?>
<h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
Pagination Like : Prev 1 2 3 Next
Would you please try above code? check my answer for more information.
For navigation you can use a plugin. Click here
and use <?php wp_pagenavi(); ?> instead of
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>

Custom Post Type showing even when taxonomy is blank

I am using Custom Post Type to get its contents by using Custom Taxonomy in different pages.
However, the contents are being displayed even if the post type has no taxonomy.
P.S. I am using ACV as well to get the fields of Taxonomy
Here's the code:
<?php if( get_field('review_category_name') ):
$taxonomy_slug = get_field('review_category_name'); //Advanced Custom Field here
endif; ?>
<?php $args = array( 'post_type' => 'reviews','tax_query' => array(
'taxonomy' => 'reviews_category',
'field' => 'slug',
'terms' => $taxonomy_slug,
), 'posts_per_page' => 5 );
$loop = new WP_Query( $args ); ?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
Am I missing anything here?
$taxonomy_slug is a variable to get the custom post type slug.
Wrap everything in the get_field('review_category_name') if check:
<?php if( get_field('review_category_name') ): ?>
<?php
$taxonomy_slug = get_field('review_category_name'); //Advanced Custom Field here
$args = array( 'post_type' => 'reviews','tax_query' => array(
'taxonomy' => 'reviews_category',
'field' => 'slug',
'terms' => $taxonomy_slug,
), 'posts_per_page' => 5 );
$loop = new WP_Query( $args );
?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
PS your example was missing the endwhile

Wordpress pagination causing WP_Query conflict

I'm using below code to display custom post type posts under template and sidebar plugin
<div class="side-peoptit">
<?php $i = 1; ?>
<?php $loop = new WP_Query(
array(
'post_type' => 'people',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'wpcf-people-sort-order',
'posts_per_page' => 4,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
)
); ?>
<?php if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post(); ?>
<?php $postid = get_the_ID(); ?>
<?php $permalink = get_permalink( $postid ); ?>
<div class="sbrpeop-title"><?php echo $i; ?>. <?PHP the_title(); ?> - <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-designation', true ) ); ?> </div>
<?php $i++; ?>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
And below code i'm using in template for pagination,
<div class="cus-pagi">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
?>
</div>
If i access website.com/people custom posts are appearing proper in both template and sidebar. But if i access second page i,e,. website.com/people/page/2 my sidebar is showing empty.
Did anyone know where i'm going wrong ?

Why is word pagination braking on page 2?

I have a categories loop that has pagination. The first page is always right. Once I get past the first page it's mixing categories together. Also, the number of pages is always wrong.
<?php
global $paged;
$cat = get_the_category();
$catSlug = $cat[1]->slug;
$query = new WP_Query(array('category_name' => $catSlug,'posts_per_page' => 20,'paged' => $paged));
?>
<?php
while ( $query->have_posts() ) :
$query->the_post();
?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
global $wp_query;
$catSlug = $cat[0]->slug;
$total_pages = $wp_query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
'add_args' => array('category_name' => $catSlug)
));
}
?>
Figured it out:
query_posts() is needed for paginate_links() to work. I had to alter the query_posts rather than use wp_query, like this:
<?php
global $query_string;
query_posts( $query_string . '&posts_per_page=20' );
?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
global $wp_query;
$total_pages = $wp_query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
));
}
?>

Display two posts from each post type with wp_query

I have used the following code to display the one post from each post.
$post_types = array('a', 's','d','f','g');//post type names
foreach( $post_types as $post_type) :
// The Query
$the_query = new WP_Query( array( 'post_type' => $post_type,
'orderby' => 'post_date',
'order' => 'DESC',
));
// The Loop
?>
<?php
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="issue-content masonItem <?php echo $post_type; ?>">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id() );
$resizedUrl = get_bloginfo('template_url')."/libs/timthumb.php?src=".$url."&w=327&h=204&zc=1"; ?>
<img src="<?php echo $resizedUrl; ?>" alt="<?php the_title(); ?>" class="masonry-thumb" />
<?php get_template_part( 'content-issues', 'page' );?>
</div><!--issue-content--><!--Mason Item-->
<?php endif; ?>
<?php endwhile;
Now i want to display 2 posts from each post type. How can i do this ? I got the $post_count idea. But i cant know use it in my code.
Try this :
'posts_per_page' => 2,
You try This
$the_query = new WP_Query( array( 'post_type' => $post_type,'orderby' => 'post_date','posts_per_page' => 2,'order' => 'DESC'));

Resources