I have a post type called "rationale" and my taxonomy name is "company_list". In taxonomy there are list of company. Each company have many rationale.
I want to get latest rationale for each company. How can i do this ?
I try below code but it show all company list but data is duplicate
<?php
//$taxonomy = 'our_work_thematic';
$myquery = array (
'post_type' => 'rationale',
'paged'=>$paged,
'posts_per_page' => -1,
);
$loop = new WP_Query($myquery);
if( $loop->have_posts() ):
while( $loop->have_posts() ):
$loop->the_post(); global $post; ?>
<?php $terms = get_the_terms( $post->ID, 'company_list' );
foreach($terms as $term) {
$termlinks = get_term_link($term);
echo '<p class="post-content--cat">';
echo '' . $term->name . '';
echo '</p>';
}?>
<?php endwhile; ?>
<?php endif; ?>
You need to get latest term and use tax_query
$args_query = array(
'post_type' => array('rationale'),
'paged' => $paged,
'posts_per_page' => -1,
);
$terms = get_terms(array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
));
if (!empty($terms) && is_array($terms)) {
$args_query['tax_query'] => array(
array(
'taxonomy' => 'company_list',
'field' => 'term_id',
'terms' => array($terms['0']->term_id), // single or array with id's
),
),
}
$query = new WP_Query($args_query);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$terms = get_the_terms($post->ID, 'company_list');
foreach ($terms as $term) {
$termlinks = get_term_link($term);
echo '<p class="post-content--cat">';
echo '' . $term->name . '';
echo '</p>';
}
}
} else {
// no post found
}
wp_reset_postdata();
try this
$terms = get_terms( array(
'taxonomy' => 'your taxonomy name',
'hide_empty' => false,
'orderby' => 'term_id',
'order' => 'asc',
) );
foreach ($terms as $terms_row) {
$terms_row->slug;
echo "<pre>";
print_r($terms_row);
echo "</pre>";
}
Thanks
Related
I have a custom shortcode to be used on archive pages and list the first 3 posts. But how do I limit the posts displayed to the current category without hardcoding categories?
This is working, but shows posts from all categories.
function archive_loop_shortcode() {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
);
$my_query = null;
$my_query = new WP_query($args);
if($my_query->have_posts()):
while($my_query->have_posts()) : $my_query->the_post();
$custom = get_post_custom( get_the_ID() );
echo "<div>".get_the_post_thumbnail()."</div>";
echo "<h3>".get_the_title()."</h3>";
endwhile;
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
}
add_shortcode( 'archive_loop', 'archive_loop_shortcode' );
Hope this code helps,
function archive_loop_shortcode() {
$current_cat = get_queried_object();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => $current_cat->taxonomy,
'field' => 'term_id',
'terms' => $current_cat->term_id,
),
),
);
$my_query = null;
$my_query = new WP_query( $args );
if ( $my_query->have_posts() ):
while ( $my_query->have_posts() ) : $my_query->the_post();
$custom = get_post_custom( get_the_ID() );
echo "<div>" . get_the_post_thumbnail() . "</div>";
echo "<h3>" . get_the_title() . "</h3>";
endwhile;
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
}
add_shortcode( 'archive_loop', 'archive_loop_shortcode' );
I want to modify this code to count posts only within certain taxonomy, for example 10 free apartments in building 1, 15 free apartments in building 2, where building is a term in taxonomy. How can I do that?
My code:
function get_post_count_by_meta( $meta_key, $meta_value, $post_type) {
$args = array(
'post_type' => $post_type,
'numberposts' => -1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'apartment_category',
'field' => 'term_id',
),
)
);
if ( $meta_key && $meta_value ) {
if ( is_array($meta_value) ) {
$args['meta_query'][] = array(
'key' => $meta_key,
'value' => $meta_value,
'compare' => 'LIKE'
);
} else {
$args['meta_query'][] = array('key' => $meta_key, 'value' => $meta_value);
}
}
$posts = get_posts($args);
$count = count($posts);
return $count;
}
while ( have_rows('floors') ) { the_row();
$post_count = get_post_count_by_meta('apartment_status', 'free', 'mieszkanie');
$floor_id = get_sub_field('floor_id');
$floor_link = esc_url(get_term_link($floor_id));
$term = get_term( $floor_id, 'apartment_category' ); ?>
<a id="floor_<?php echo $i; ?>" href="<?php echo $floor_link; ?>" class="floor-number">
<span class="build-name"><?php echo $term->name; ?></span>
<span><?php echo $post_count; ?> available apartments</span>
</a>
<?php $i++;
}
I can't get pagination to work with custom post types.
It's showing up correctly, when I click it goes to /page/2 and /page/3 etc, and all those give a 404 Page not found error.
index.php
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array('name1', 'name2', 'name3', 'post'),
'posts_per_page' => 4,
'post_status' => 'publish',
'paged' => $paged
);
$query = new WP_Query( $args );
?>
<?php echo $paged; ?>
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
// do stuff here
<?php the_excerpt(); ?>
<?php endwhile; ?>
// get pagination
<?php mypagination( $query ); ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
functions.php
the built in the_posts_pagination() doesn't output anything.
function mypagination($query){ ?>
<ul class="pagination">
<?php
$pages = paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => true,
'type' => 'array',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => '<span class ="fa fa-caret-left" aria-hidden="true"></span><span class="prev-text">Prev</span>',
'next_text' => '<span class="next-text">Next</span> <span class ="fa fa-caret-right" aria-hidden="true"></span>',
'add_args' => false,
'add_fragment' => '',
) );
if (is_array($pages)):
foreach ($pages as $p): ?>
<li class="pagination-item js-ajax-link-wrap">
<?php echo $p; ?>
</li>
<?php endforeach;
endif; ?>
</ul>
<?php
}
If I create a custom API endpoint with the same query/arguments, pagination IS working.
/**
* Custom JSON API endpoint.
* Endpoint URL http://localhost/websitename/wp-json/frontpage/v1
*/
function api_frontpage_posts($data) {
$output = array();
// Get post slug.
$post = get_post($data['parent_id']);
$slug = $post->post_name;
// e.g. 1, 2, 3,...
$paged = $data['page_number'] ? $data['page_number'] : 1;
$query_args = array(
'post_type' => array('name1', 'name2', 'name3', 'post'),
'post_status' => array('publish'),
'posts_per_page' => 4,
'paged' => $paged,
);
// Create a new instance of WP_Query
$the_query = new WP_Query($query_args);
if (!$the_query->have_posts()) {
return null;
}
while ($the_query->have_posts()) {
$the_query->the_post();
$post_id = get_the_ID();
$post_title = get_the_title();
$post_content = get_the_content();
$post_excerpt = get_the_excerpt();
$post_date = get_the_date('l, j F Y');
// Get the slug.
$post = get_post($post_id);
$post_slug = $post->post_name;
// Get image alt.
$alt = get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_alt', true);
$desc = get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_caption', true);
// Get image description.
$description = null;
$thumbnail = get_posts(array('p' => get_post_thumbnail_id(), 'post_type' => 'attachment'));
if ($thumbnail && isset($thumbnail[0])) {
$description = $thumbnail[0]->post_content;
}
// Image data.
$post_image = array(
'id' => get_post_thumbnail_id() ,
'url' => get_the_post_thumbnail_url(),
'caption' => get_the_post_thumbnail_caption(),
'alt' => $alt,
'description' => $description,
);
// Category data.
$categories = get_the_category($post_id);
$post_categories = array();
foreach ($categories as $key => $category) {
$post_categories[] = array(
'name' => $category->name,
'slug' => $category->slug,
'url' => get_category_link($category->cat_ID),
);
}
// Push the post data into the array.
$output[] = array(
'id' => "post" . $post_id, // cannot use numbers, or hyphens between words for Zurb data toggle attribute
'slug' => $post_slug,
'title' => $post_title,
'url' => get_permalink($post_id),
'date' => $post_date,
'content' => $post_content,
'excerpt' => $post_excerpt,
'image' => $post_image,
'categories' => $post_categories
);
}
$result = array(
"next" => (int)$paged === (int)$the_query->max_num_pages ? null : site_url() . '/' . $slug . '/page/' . ($paged + 1) . '/',
"prev" => (int) $paged === 1 ? null : site_url() . '/' . $slug . '/page/' . ($paged - 1) . '/',
// Split the array every 4 items.
"chunks" => array_chunk($output, 4)
);
// Reset the post to the original after loop. otherwise the current page
// becomes the last item from the while loop.
wp_reset_query();
// Return json.
return $result;
}
add_action('rest_api_init', function () {
register_rest_route('frontpage/v1', '/page/(?P<page_number>\d+)', array(
'methods' => 'GET',
'callback' => 'api_frontpage_posts',
));
});
(hooray Reddit)
Someone pointed me to the fix.
Add this to functions.php too, via https://wordpress.stackexchange.com/questions/22528/custom-post-type-pagination-404-fix
add_action( 'parse_query','changept' );
function changept() {
if( is_category() && !is_admin() )
set_query_var( 'post_type', array( 'post', 'your_custom_type' ) );
return;
}
I am trying to display posts of specific category(taxonomy) that is 'Book1'.
I tried to display it with the following code.
$args = array(
'post_type' => 'book',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'Book1',
'field' => 'id',
'terms' => 1
)
)
);
echo '<br>';
$postss = get_posts( $args );
if ( ! empty( $postss ) && is_array( $postss ) ) {
// Run a loop and print them all
$i=1;
foreach ( $postss as $termm ) {
echo ' '.$i.' '.$termm->post_title. '<br>';
$i++;
}
}
?>
In output no any item is displayed.
$custom_terms = get_terms('Book1');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array(
'post_type' => 'book',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'Book1',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
}
try this code
$args = array(
'post_type' => 'book',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'Book1',
'field' => ''term_id', // here you are worng name too
'terms' => 1
)
)
);
echo '<br>';
$postss = get_posts( $args );
if ( ! empty( $postss ) && is_array( $postss ) ) {
// Run a loop and print them all
$i=1;
foreach ( $postss as $termm ) {
echo ' '.$i.' '.$termm->post_title. '<br>';
$i++;
}
}
?>
// best solution
<?php
$query = new WP_Query( array(
'post_type' => 'book', // name of post type.
'tax_query' => array(
array(
'taxonomy' => 'Book1', // taxonomy name
'field' => 'term_id', // term_id, slug or name
'terms' => 1, // term id, term slug or term name
)
)
) );
while ( $query->have_posts() ) : $query->the_post();
// do stuff here....
endwhile;
/**
* reset the orignal query
* we should use this to reset wp_query
*/
wp_reset_query();
?>
I did it by doing like this. Thanks all.
$args = array(
'post_type' => 'book',
'cat' => '35'
);
echo '<br>';
$postss = query_posts($args);
if ( ! empty( $postss ) && is_array( $postss ) ) {
// Run a loop and print them all
?><?php $i=1;
foreach ( $postss as $termm ) { ?>
<?php echo ' '.$i.' '.$termm->post_title. '<br>';$i++;?>
<?php
}
}
I would like to display categories (from taxonomy-product_category) in archive-product.php.
Get categories:
Fruit
Herbs
Salad
Vegetables
See screenshot what I mean:
When I added coding in archive-product.php:
<?php
$args = array(
'post_type' => 'product',
'taxonomy' => 'product_category',
'hierarchical' => 1,
'nopaging' => false,
'posts_per_page' => '2',
'posts_per_archive_page' => '10',
'ignore_sticky_posts' => true,
'order' => 'rand',
);
echo '<div class="container">';
echo '<div class="row">';
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="col-lg-4">';
echo '<a href="'.get_the_permalink().'">';
if ( has_post_thumbnail() ) {
the_post_thumbnail(array(486,226));
}
the_title();
the_content();
echo '</a>';
echo '</div>';
}
} else {
// no posts found
echo wpautop( 'Sorry, no posts were found' );
}
echo '</div>';
echo '</div>';
// Previous/next post navigation.
previous_post_link( '%link', 'Prev post in category', true
);
next_post_link( '%link', 'Next post in category', true );
// Restore original Post Data
wp_reset_postdata();
?>
But not display categories (Fruit, Herbs, Salad, Vegetables)
Would anyone know about this?
Thanks,
Shaun.
Please try below code:
$args = array(
'taxonomy'=> 'product_category',
'order' => 'DESC',
);
$categories = get_categories($args);
print_r($categories);
You can get the list of all product categories using Below code
$orderby = 'name';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '
<ul>';
foreach ($product_categories as $key => $category) {
echo '
<li>';
echo '<a href="'.get_term_link($category).'" >';
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>
';
}