I have a custom taxonomy template taxonomy-event-category.php and i
have added following code for pagination but it does not work.
<?php
$current_term = single_term_title("", false);
$cat_id = get_cat_ID('My Category');
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args=array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 1,
'paged'=>$paged,
'caller_get_posts'=> 1,
'tax_query' => array(
array(
'taxonomy' => 'event-categories',
'field' => 'slug',
'terms' => $current_term,
),
),
);
$new = new WP_Query($args);
while( $new->have_posts() ): $new->the_post();
/* OUTPUT HERE */
endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $new ) );
}
wp_reset_postdata();
?>
whenever i click on the next page or second page getting me 404
page not found error.
Anyone have idea about how pagnavi work in custom taxonomy template.
Related
I have created a custom post type called 'stories', with categories. there are 5 categories, one of them is 'Fostering'.
I want to pull in all custom 'stories' posts with the category of 'fostering' into a page, I currently have this in my functions.php
function register_shortcodes() {
add_shortcode( 'stories', 'stories_function' );
}
add_action( 'init', 'register_shortcodes' );
function stories_function($atts) {
global $wp_query,
$post;
$atts = shortcode_atts( array(
'cat' => ''
), $atts );
$loop = new WP_Query( array(
'posts_per_page' => 3,
'post_type' => 'stories',
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( sanitize_title( $atts['cat'] ) )
) )
) );
if( ! $loop->have_posts() ) {
return false;
}
while( $loop->have_posts() ) {
$loop->the_post();
echo the_title();
echo the_content();
}
wp_reset_postdata();
}
and the shortcode I am using is [stories category="fostering"] but nothing is being pulled in, I have dummy data in this custom post type with the category of 'Fostering' applied.
Try this code as you missed to pass cat => Fostering and make sure Fostering is slug of category else pass correct slug of that category
function register_shortcodes() {
add_shortcode( 'stories', 'stories_function' );
}
add_action( 'init', 'register_shortcodes' );
function stories_function($atts) {
global $wp_query,
$post;
$atts = shortcode_atts( array(
'cat' => 'fostering'
), $atts );
$loop = new WP_Query( array(
'posts_per_page' => 3,
'post_type' => 'stories',
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'story_category',
'field' => 'slug',
'terms' => array( sanitize_title( $atts['cat'] ) )
) )
) );
if( ! $loop->have_posts() ) {
return false;
}
while( $loop->have_posts() ) {
$loop->the_post();
echo the_title();
echo the_content();
}
wp_reset_postdata();
}
In my website, I have two different post-types. One of them is publication with custom category-type as publication-category and the other is service with custom category-type as service-category. I am publishing some brochures in Publication page those are under different services. What I am trying to do is displaying these brochures in Services page by same category type. It is like, if the brochure is published by Education services, then this brochure should be displayed in Education service page.
I am currently using ACF plugin to do so, but whenever there is new brochure, I have to go to service page and add it there. Today I tried below code but it displays all brochures from different category-types not the same category-type.
Perhaps you guys can help me how I can arrange the code a way that works for my above request.
<?php
$custom_taxterms = wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'id',
'terms' => $custom_taxterms
)),
'post__not_in' => array( $post->ID ),
);
$related_items = new WP_Query( $args );
if ( $related_items->have_posts() ) :
echo '<ul>';
while ( $related_items->have_posts() ) : $related_items->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
endif;
wp_reset_postdata();
?>
If you're on services page, so why do you use the 'publication-category' in
wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
Seems like you have to use
$custom_taxterms = get_the_terms($post->ID, 'service-category');
$terms = [];
foreach ($custom_taxterms as $term) {
$terms[] = $term->slug
}
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $terms
)),
);
And create same slugs for both terms in both categories. From my understanding. It's quite difficult to understand your architecture
I find the solution by changing service-category taxonomy to publication-category as same with other post-type and creating relationship with below code from : https://wordpress.stackexchange.com/questions/139571/display-posts-with-same-taxonomy-term?rq=1
Thanks everyone
<?php
//get the post's venues
$custom_terms = wp_get_post_terms($post->ID, 'publication-category');
if( $custom_terms ){
// going to hold our tax_query params
$tax_query = array();
// add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
if( count( $custom_terms > 1 ) )
$tax_query['relation'] = 'OR' ;
// loop through venus and build a tax query
foreach( $custom_terms as $custom_term ) {
$tax_query[] = array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// put all the WP_Query args together
$args = array( 'post_type' => 'publication',
'posts_per_page' => 20,
'tax_query' => $tax_query );
// finally run the query
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="listing-title"><?php the_title(); ?></div>
<div class="listing-image">
</div>
<?php
endwhile;
}
wp_reset_query();
}?>
I have a custom post type called criticos and I'm having problems with pagination.
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 8,
'post_type' => 'avaliacoes',
'paged' => $paged,
//'orderby' => 'meta_value_num',
//'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'critico', // name of custom field
'value' => '"' . $Dados['id'] . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
),
));
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
foreach ($loop->posts as $avaliacao) {
//Do Stuff Here
}
}
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
The pagination is not working when go to the page 2 redirect to first page.
My page is single-criticos
PS: English isn't my first language.
I have a custom page template with products in Woocommerce, but I want order the products by attribute's terms name asc. I have three terms. I don't know the right way to do it, I have tried this:
<?php
args = array(
'post_type' => 'product',
'posts_per_page' => 20,
'meta_key' => 'pa_tire-type',
'orderby' => 'meta_value_num',
'order' => 'asc'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
— Updated —
You need to add a tax_query for your product attribute with the related terms (slugs) in it this way:
// Set HERE the attibute taxonomy slug
$taxonomy = 'pa_tire-type';
// Set HERE the term slugs for this attribute
$terms_array = array('term_slug1', 'term_slug2', 'term_slug3');
// The loop query
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 20,
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $terms_array,
'operator' => 'IN',
) ),
'order_by' => 'terms',
'order' => 'asc'
) );
$product_ids = array();
if ( $loop->have_posts() ): while ( $loop->have_posts() ) : $loop->the_post();
$product_ids[] = $loop->post->ID;
endwhile;
wp_reset_postdata();
endif;
// Testing output
print_pr($product_ids);
This code is tested and works.
I am trying to display a custom post type that has custom taxonomy, but I am not having any luck. Nothing is showing up. I appreciate any help.
Post type = gallery
Custom Taxonomy slug = photoarea
The 'photoarea' I want to display = fourth
<?php
$args = array(
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'photoarea',
'field' => 'fourth',
)
),
'posts_per_page' => 10,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_post_thumbnail();
endwhile; endif;
wp_reset_query();
?>
You may use below code snippet:
$the_query = new WP_Query( 'post_type=gallery&photoarea=fourth');
and then your while loop.
if my understanding is right, that you need to get the custom taxonomy with following code,
instead of field you must use term to get the posts in Fourth
<?php
$args = array(
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'photoarea',
'field' => 'slug',
'terms' => 'fourth'
)
),
'posts_per_page' => 10,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_post_thumbnail();
endwhile; endif;
wp_reset_query();
?>
$args = array('post_type' => 'gallery','posts_per_page'=>'-1','tax_query' => array(array(
'taxonomy' => 'photoarea',
'field' => 'term_id',
'terms' => $your_term_id,
),
),
);