2 wp querys clashing with pagination - php

I have been trying to display a featured post and also the latest posts on the same page but when I go to use pagination, it shows /page/2 in the permalink but it shows the same posts as page 1. I have a feeling the querys are conflicting! How do I fix this please? Code below
<?php query_posts( array(
'post_type' => array('event-', 'videos', 'videos-', 'audio', 'audio-'),
'posts_per_page' => 1,
'meta_key' => '_featured',
'meta_value' => 'yes'
));
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="potdpostcon">
<div class="featuredimgpotd">
<div class="featuredpostimgzoom"></div><?php the_post_thumbnail( 'featured-post-home' ); ?>
</div>
<div class="featuredbasepotd">
<div class="desclrgexcon"><span class="postexplrg"><?php the_titlesmall('', '...', true, '82') ?></span></div>
<div class="goiconlargecon"><img src="/wp-content/uploads/2014/06/goicon_lrg.png" />
<?php endwhile; endif; ?>
2nd wp query:
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array( 'post', 'videos', 'videos-', 'audio-', 'event-'),
'showposts' => 80,
'paged' => $paged
);
$wp_query = new WP_Query( $args );
if ( have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php
if( $current_date != get_the_date('d-m-Y') ){
echo '<div class="datetitle100spc"><div class="datetitleconspc"></div></div><div class="datetitle100"><div class="datetitlecon"><span class="dateheadertitle">'.get_the_date('l dS / M Y').'</span></div></div>';
$current_date = get_the_date('d-m-Y');
}
?>
<div class="postmainbase">
<div class="postexcon">
<div class="featuredimgexcon"><div class="postcategoryex"><div class="postcategorytitle"><span class="catexsmall"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></span></div></div><div class="featuredimgzoom"></div><?php the_post_thumbnail( 'home-posts' ); ?></div>
<div class="sepexcon"></div>
<div class="descexcon"><span class="postexp"><?php the_titlesmall('', '...', true, '92') ?></span></div>
<div class="goexcon"><img src="/wp-content/uploads/2014/06/goicon.png" /></div>
</div>
</div>
<?php endwhile; ?>
<div class="paginationdiv">
<?php wp_pagenavi(); ?>
</div>
<?php endif; ?>

Related

Why isnt my pagination working in the while post loop?! Help would be much appreciated, new to PHP

I have this code down below, showing 16 posts and on the next page the next 16 but right now it's not getting new posts on the second + page. It is showing the same first 16 that it shows on the first page. I don't know where I went wrong and why its not communicating with the while loop?
$cat = get_the_category();
$cat_name = esc_html($cat[0]->name);
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => $cat_name,
'posts_per_page' => 16,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
'offset' => 5
);
$all_catPosts = null;
$all_catPosts = new WP_Query($args);
if ($all_catPosts->have_posts()) {
?>
<!-- 16 category container with the next page pagination button -->
<div class="min-h-screen flex items-center justify-center">
<div class="grid grid-cols-4">
<?php
while ($all_catPosts->have_posts()): $all_catPosts->the_post();?>
<div class="p-5 rounded big_cat_container">
<!-- // all content, title, meta fields-->
<section><a class="thumbnail_img" href="<?php the_permalink();?>"><?php the_post_thumbnail();?></a></section>
<h4 class="post_title"><?php the_title();?></h4>
</div>
<?php endwhile;?>
</div><!--/ end of grid-->
<!-- //pagination buttons -->
<?php
$previousLink = get_previous_posts_link();
$nextLink = get_next_posts_link();
$hasNextPage = $previousLink || $nextLink;
if ($hasNextPage):?>
<nav class="pagination" role="navigation">
<?php if($previousLink || $paged > 1 ) { ?>
<div class="page-btn nav-next">
<button class="previous-btn"><?php previous_posts_link( 'Previous Page', ); ?></button>
</div>
<?php }
if($nextLink) { ?>
<div class="page-btn nav-previous">
<button class="next-btn">
<?php next_posts_link( 'Next Page ', $the_query->max_num_pages );?>
</button>
</div>
<?php } ?>
</nav>
<?php endif;?>
<!-- end pagination -->
</div><!--/ end of container-->
<?php } ?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php
$cat = get_the_category();
$cat_name = esc_html($cat[0]->name);
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => $cat_name,
'posts_per_page' => 16,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
'offset' => 5
);
$all_catPosts = null;
$all_catPosts = new WP_Query($args);
if ($all_catPosts->have_posts()) {
?>
<!-- 16 category container with the next page pagination button -->
<div class="min-h-screen flex items-center justify-center">
<div class="grid grid-cols-4">
<?php
while ($all_catPosts->have_posts()): $all_catPosts->the_post();?>
<div class="p-5 rounded big_cat_container">
<!-- // all content, title, meta fields-->
<section><a class="thumbnail_img" href="<?php the_permalink();?>"><?php the_post_thumbnail();?></a></section>
<h4 class="post_title"><?php the_title();?></h4>
</div>
<?php endwhile;?>
</div><!--/ end of grid-->
<!-- //pagination buttons -->
<?php
$previousLink = get_previous_posts_link();
$nextLink = get_next_posts_link();
$hasNextPage = $previousLink || $nextLink;
if ($hasNextPage):?>
<nav class="pagination" role="navigation">
<?php if($previousLink || $paged > 1 ) { ?>
<div class="page-btn nav-next">
<button class="previous-btn"><?php previous_posts_link( 'Previous Page', ); ?></button>
</div>
<?php }
if($nextLink) { ?>
<div class="page-btn nav-previous">
<button class="next-btn">
<?php next_posts_link( 'Next Page ', $all_catPosts->max_num_pages );?>
</button>
</div>
<?php } ?>
</nav>
<?php endif;?>
<!-- end pagination -->
</div><!--/ end of container-->
<?php } ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 16,
'paged' => $paged
);
if ( $the_query->have_posts() ) :
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
// next_posts_link() usage with max_num_pages.
next_posts_link( __( 'Older Entries', 'textdomain' ), $the_query->max_num_pages );
previous_posts_link( __( 'Newer Entries', 'textdomain' ) );
// Clean up after the query and pagination.
wp_reset_postdata();
else:
?>
<p><?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' ) ); ?></p>
<?php
endif;

Wordpress page content and posts in the same page

I'm pretty new in world of wordpress and I'm trying to adjust HTML page into wordpress theme.
I need a page content to be displayed first on a page and under that, the posts should be shown. But what I'm getting are just posts shown twice on the page (where page content should be). Is there any possibility to overcome this?
And additional question, how to filter posts according to their category? I've tried with query_posts('cat=Small'), but it doesn't seem to work properly.
The code for index.php looks as following:
<?php get_header(); ?>
<?php
wp_reset_query();
while ( have_posts() ) : the_post();
the_content();
endwhile;
wp_reset_query();
?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php query_posts('cat=Small'); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
Try the below code. This may help you
<section>
<div class="major">
<h2>Erat lacinia</h2>
</div>
<div class="features">
<?php $args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'ASC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);?>
<?php query_posts( $args ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?php the_title(); ?></h3>
<p><?php the_content('Read More'); ?></p>
</div>
</article>
<?php endwhile; wp_reset_query(); ?>
</div>
</section>
You can use two loops.
In your php page template, first execute the regular loop to get the content of the actual page, like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//output page content here
<?php endif; ?>
Then you define a new query for the desired posts:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'ASC',
)
);
//(Add and change arguments as desired in the array above)
$loop1 = new WP_Query($args);
if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post();
//Output the post contents in a loop here
<?php endif;
wp_reset_postdata();?>
And then add the rest of the page template (footer etc.)
<?php
/*
*Template name: test
*/
get_header();
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$attrs = array(
'numberposts' => 10,
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'small' )
)
)
);
$my_posts = get_posts( $attrs );
the_content();
?>
<?php if ($my_posts): ?>
<section>
<header class="major">
<h2>Erat lacinia</h2>
</header>
<div class="features">
<?php foreach ($my_posts as $key => $value): ?>
<article>
<span class="icon fa-diamond"></span>
<div class="content">
<h3><?= $value->post_title; ?></h3>
<p><?= $value->post_content ?></p>
</div>
</article>
<?php endforeach ?>
</div>
</section>
<?php endif ?>
<?php
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
get_footer(); ?>

post by taxonomy not showing at all in wordpress

Newbie here,
I'm trying to display the list of my post depends on the category. But it doesn't display my post. I tried the different type of array, but no luck. I named my page taxonomy-blog_category.php
I call the page via site.com/blog_category/category
here's my current code and I know I'm so close but can't figure it out.
Here is the array:
<div class="row">
<?php $ctr = 1;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'blog_post',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'blog-category',
'field' => 'slug',
'terms' => array('business','people','technology'),
),
),
);
Here is how I display the post
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="col-md-4 text-center">
<div class="content-container">
<div class="wrap">
<figure class="tint t2">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240">
</figure>
</div>
<h2><?php the_title(); ?></h2>
<h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3>
<?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...');?>
<div class="read-more-btn">
read more
</div>
</div>
</div>
<?php $ctr++; endwhile; ?>
I Don't know if this is necessary but here's my code for pagination:
<div class="pagination-holder">
<ul class="pagination">
<?php
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</ul>
</div>

Tag template instead of 'post' in the following loop

I'm using the code (loop) below for the blog post grid displaying. I want the following structure for my tag.php as well (the post by the tag instead of all blog posts). But I don't know how to do it. Could you, please, check it?
I can't remember how I solved the following problem in the past but I'm at a 100% sure that I found the solution previously.
<?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(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'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="item col-sm-4">
<div class="well">
<h2><?php the_title(); ?></h2>
<span class="date"><?php echo get_the_date("j.n.Y"); ?></span>
<a href="<?php the_permalink(); ?>"><?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<?php the_excerpt(); ?>
<div class="readmore-wrapper">
<a class="readmore" href="<?php the_permalink(); ?>">Suite</a>
</div>
</div></div>
<?php endwhile; ?>
</div>
<?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="next-posts-link">
<?php echo get_previous_posts_link( '< Page précédente' ); ?>
</div>
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Page suivante >', $custom_query->max_num_pages ); ?>
</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;
?>
Please check WP_Query with Tag Parameter
https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
example:
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date', // modified | title | name | ID | rand
'tag' => 'tag1,tag2'
);

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