WordPress Taxonomy, listing ONLY the single taxonomies - php

So I have a custom Taxonomy 'Sectors'.
I am trying to display all the single posts in 'Sectors' in a list, just a basic list.
I currently do have it so, it will display the category, then a sub category, then the list from there.
I also have this, which lists all posts - (NOT TAXONOMY) :
<?php
$uncat = get_cat_ID('uncategorised');
$args = array(
'posts_per_page' => 5,
'category__not_in' => array($uncat)
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-12 col-sm-12 col-xs-12" style="padding-left:0; padding-right:0;">
<a href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div class="leftfloat" style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<div class="contentfaq">
<h3><?php the_title(); ?></h3>
<span class="entry-date-orange"><strong><?php echo get_the_date(); ?></strong></span>
<?php
foreach((get_the_category()) as $category) {
echo ' | ' . $category->cat_name;
}
?>
<p style="margin-top:10px";><?php the_excerpt(); ?></p>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
is there a way, to use this code below, to ONLY get items from the taxonomy 'sectors'?

For this to work, you will need to get all the terms belonging to your taxonomy, and then use a tax_query to query the correct posts. For effeciency, we will only get the term ids and not the complete objects
(NOTE: The code is untested and requires PHP 5.4+. Also, just confirm the taxonomy name)
$term_ids = get_terms( 'SectorCategories', ['fields' => 'ids'] );
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
$args = [
'post_type' => 'sectors',
'posts_per_page' => 5,
'paged' => $paged,
'tax_query' => [
[
'taxonomy' => 'SectorCategories',
'terms' => $term_ids,
]
],
// REST OF YOUR ARGUMENTS
];
$loop = new WP_Query( $args );
EDIT
For older PHP versions, use the following code
$term_ids = get_terms( 'SectorCategories', array( 'fields' => 'ids' ) );
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
$args = array(
'post_type' => 'sectors',
'posts_per_page' => 5,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'SectorCategories',
'terms' => $term_ids,
)
),
// REST OF YOUR ARGUMENTS
);
$loop = new WP_Query( $args );
EDIT 2
A very basic loop for the above query would look something like this
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) {
$loop->the_post();
the_title();
}
next_posts_link( 'Next posts', $loop->max_num_pages );
previous_posts_link( 'Previous posts' );
wp_reset_postdata();
}

Related

Wordpress - Pagination doesn't work with custom loop but working in blog pages

I have a loop with custom post types, and pagination doesn't appear, when I enter the URL with /page/2, /page/3... it shows the content correctly, but links don't appear on the page.
Here is the code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$parent_only_query = new WP_Query(array(
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
));
while ($parent_only_query->have_posts()){
$parent_only_query->the_post();
//content
}
pagination(); ?>
Archive page with pagination working:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php //content ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'loop-templates/content', 'none' ); ?>
<?php endif; ?>
<?php pagination(); ?>
add this in your functions.php
function pagination_nav() {
global $wp_query;
if ( $wp_query->max_num_pages > 1 ) { ?>
<nav class="pagination" role="navigation">
<div class="nav-previous"><?php next_posts_link( '← Older posts' ); ?></div>
<div class="nav-next"><?php previous_posts_link( 'Newer posts →' ); ?></div>
</nav>
<?php }
}
display on page.php
<?php pagination_nav(); ?>
pagination can be shown in custom post type archive template and on custom template as well.
Pagination for archive template.
// current page
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array( 'post_type' => 'product',
'post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
new WP_Query( $args );
// Call pagination function before wp_reset_postdata()
the_posts_pagination( array(
'prev_text' => '<span class="fa fa-angle-left" aria-hidden="true"></span>',
'next_text' => '<span class="fa fa-angle-right" aria-hidden="true"></span>',
'screen_reader_text' => ' ',
'before_page_number' => '',
'mid_size' => 3,
) );
Pagination for custom Template
// Get current page.
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// prepare arguments
$args = array(
post_type' => 'my_cpt',
'posts_per_page' => 4,
'paged' => $paged,
'post_parent' => 0
);
//prepare query
$query = new WP_Query( $args );
$totalPage=$query->max_num_pages;
$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' => $totalPage
) );
You can check WordPress official document on Wordpress Codex

Excluding a Taxonomy From Post Type Loop Separated by Categories

I'm trying to display a post type’s categories and output the posts within that category, but I have another taxonomy that I want to exclude. This code is showing all Taxonomies and Terms registered to the specific post type.
The second taxonomy that I am trying to exclude is named "manufacturer" and is registered to multiple post types. I have tried doing 'operator'=>'NOT EXISTS' as well as what you see below, with the if...continue but nothing seems to work !!
<?php
$post_type = 'equipment';
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
foreach( $terms as $term ):
if ( $term->slug == 'manufacturer' )
continue;
?>
<div class="row">
<div class="col-xs-12">
<h2><?php echo $term->name; ?></h2>
</div>
<div class="col-xs-12">
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<h4><?php echo get_the_title(); ?></h4>
<?php endwhile; endif; ?>
</div>
</div>
<?php endforeach;
endforeach; ?>
)
$args = array(
'post_type' => array('equipment'),
'posts_per_page' => -1,
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'manufacturer',
'field' => 'slug',
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );

pagination not working for category of custom post type

When I use pagination for custom post type product its working fine but its not working for the categories of custom post type. for ex. pagination working for this http://localhost/wordpress/products/page/2/ and not for this http://localhost/wordpress/products/landscape/page/2/ its always showing the first page content. How to solve this? given below is my code.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'taxonomy' =>'product_cat','term' => $cat_name1,'orderby'=>'post_date','page'=>$paged );
$wp_query = new WP_Query($args);
if($wp_query->have_posts()) : while ($wp_query->have_posts()): $wp_query->the_post();
<div class="product_list">
<?php the_title();?>
</div>
<?php endwhile; ?>
<?php wp_pagenavi( array( 'query' => $wp_query ) );//plugin code ?>
<?php else : ?>
<!-- No posts found -->
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php echo "No Products found for this categoy!." ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
Try this :
-Replace 'page' arguments with 'paged'
-Replace 'taxonomy' with 'tax_query'.
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$taxonomy = 'product_cat';
$taxonomy_terms = get_terms( $taxonomy, array(
'hide_empty' => 0,
'fields' => 'ids'
) );
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $taxonomy_terms,
),
),'orderby'=>'post_date','paged'=>$paged );
In your question, you have used $cat_name1 for terms listing then please use following code:
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$taxonomy = 'product_cat';
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $cat_name1,
),
),'orderby'=>'post_date','paged'=>$paged );
Pagination :
please replace wp_pagenavi() function with following code:
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => max( 1, $paged ),
'total' => $wp_query->max_num_pages
) );

Woocommerce custom products list pagination

I have code like this for fetching and displaying products in woocommerce:
{
$args = array(
'post_type' => 'product',
'posts_per_page' => 30
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//display results here;
endwhile;
}
The problem is while the code displays 30 products at a time as specified, it does not add page links so other products could be viewed, is there a way to do this or am I missing somethin?
<ul class="products">
<?php
global $paged;
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'paged' => $paged
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
<?php wp_reset_postdata(); ?>
</ul><!--/.products-->
Reference : Pagination in WooCommerce
Can you please check below code? i hope this code is work for you.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'product',
'posts_per_page' => 30,
'paged' => $paged,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//display results here;
endwhile;
$total_pages = $loop->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 »'),
));
}
endif;
wp_reset_postdata();

Nest a while loop in an if else statement in WordPress

I have a while loop that gets all pages of a certain category on index.php which is my posts page.
However, I want to change the code below, so if it is the homepage, it does not run this loop but displays some text.
This code the loop works:
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
while ( $loop->have_posts() ) : $loop->the_post();
?> <?php the_title();?>
endwhile;
?>
In this code, nothing loads on any page except the homepage:
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC' ,
'paged'=>$paged
);
if ( is_home() ) {
echo 'Welcome!';
} else if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
You need to wrap your query and loop in your is_home() condition
if ( !is_home() ) { // This is not the home page
// Add your query and loop here
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
while ( $loop->have_posts() ) : $loop->the_post();
?> <?php the_title();?>
endwhile;
} else { // This is the homepage
echo 'Some text here';
}
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $the_query->the_post();
if ( is_home() ) {
echo 'Welcome!';
}
else{?>
<h2><a href="<?php echo get_permalink(); ?>"><?php the_title();
}
<?php endwhile; endif; ?>

Categories