So I have a custom type called ("knowledge_base") which has a taxonomy called ('section') and one of them is ('dog bite'). Right now I am on example.com/section/dog-bite/ and I am trying to show the posts located here. This is what I have so far so I am not sure what is missing but it just displays ALL the posts from all sections.
$current = get_queried_object();
$args = array(
'post_type' => 'knowledge_base',
'tax_query' => array(
array(
'taxonomy' => 'section',
'field' => $current->slug,
'terms' => $current->name
)
)
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
There is supposed to be only 2 posts
Check this code.
$args = array(
'post_type' => 'knowledge_base',
'tax_query' => array(
array(
'taxonomy' => 'section',
'field' => 'slug', // ‘term_id’, ‘name’, ‘slug’ or ‘term_taxonomy_id’
'terms' => $current->slug, // It's will be $term->slug
)
)
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
Related
Im using this php code on my page to show subcategories, and when click on certain subcategory should redirect to posts that have that subcategory:
<?php
wp_list_categories( array(
'taxonomy' => 'categorys',
'title_li' => '',
'child_of' => get_term_by( 'slug', 'osten', 'categorys' )->term_id,
) );
?>
I created taxonomy.php
I have 6 categories to filter these posts and each Category have its subcategories.
SO i need to filter posts, when i click on subcategory to show me only posts on it.
I used this code inside taxonomy.php but its showing me all of posts and not filtering by clicked subcategory:
<?php
$post_type = 'ortemp';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies($post_type); // names (default)
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
echo '<div id="activities_categories">';
foreach( $terms as $term ) :
$customposts = new WP_Query(
array(
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => $term->slug,
'field' => 'slug',
// 'category' => 'category-1'
)
)
)
);
if( $customposts->have_posts() ):
while( $customposts->have_posts() ) : $customposts->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
endif;
endforeach;
echo '</div>';
endforeach;
?>
try these code to your 'taxonomy.php' file.
$currentTerm = get_queried_object();
$subTerms = get_term_children( $currentTerm->term_id, $currentTerm->taxonomy );
if ( ! empty( $subTerms ) ) {
echo '<div id="activities_categories">';
foreach ( $subTerms as $subTerm ) :
$customposts = new WP_Query(
array(
'posts_per_page' => - 1,
'tax_query' => array(
array(
'taxonomy' => $currentTerm->taxonomy,
'terms' => $subTerm,
'field' => 'term_id',
)
)
)
);
if ( $customposts->have_posts() ):
while ( $customposts->have_posts() ) : $customposts->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
endif;
endforeach;
echo '</div>';
}
another example
$currentTerm = get_queried_object();
$subTerms = get_term_children( $currentTerm->term_id, $currentTerm->taxonomy );
if ( ! empty( $subTerms ) ) {
echo '<div id="activities_categories">';
$customposts = new WP_Query(
array(
'posts_per_page' => - 1,
'tax_query' => array(
array(
'taxonomy' => $currentTerm->taxonomy,
'field' => 'term_id',
'terms' => $subTerms,
)
)
)
);
if ( $customposts->have_posts() ):
while ( $customposts->have_posts() ) : $customposts->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
endif;
echo '</div>';
}
I have a product category called Cable, and I want to grab all posts assigned to this category and output the title of said posts. This category is from the custom taxonomy 'Category' that woo-commerce adds, I'm not sure if that makes things harder? but I haven't found a solution yet.
Could anyone assist with this?
Try this code:
And modify as per your requirements:
add_shortcode( 'specific_category_posts', 'kb_get_posts' );
function kb_get_posts() {
$query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( '18' ), //Your custom category id
),
),
)
);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
}
}
wp_reset_postdata();
}
Use this code to show all posts assigned to the category cable
<?php
global $post;
$args = array( 'numberposts' => 10, 'category_name' => 'cable' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post);
?>
<div>
<?php
the_title();
the_excerpt(); ?>
</div>
<?php endforeach; ?>
I have created with list
add_shortcode( 'pi_cable_posts', 'pi_posts' );
function pi_posts() {
$pi_query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1, //--1 for unlimited and number to limit
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( 'YOUR ID HERE' ), //ADD YOUR ID HERE
),
),
));
if ( $pi_query ->have_posts() ) {
echo '<ul>';
while ( $pi_query ->have_posts() ) {
$pi_query ->the_post();
echo '<li><h2><a href="'.get_the_permalink().'">' . get_the_title() . '</h2></li>';
}
echo '</ul>';
}
wp_reset_postdata();}
You can use WP_Query() with tax_query().
EDIT:
Try out this code to get products on category page.
add_shortcode('product_list', 'zillion_show_products_title_by_cat', 10);
function zillion_show_products_title_by_cat()
{
if(!is_product_category()){
return;
}
$term = get_queried_object();
ob_start();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term->term_id, // 21 is category id When you have more term_id's seperate them by comma.
'operator' => 'IN'
)
);
$the_query = new WP_Query($args);
// The Loop
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
echo '<h3>' . get_the_title() . '</h3>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$contents = ob_get_clean();
return $contents;
}
i want to get the author's posts in spacific category and display it in author page
I tried this code and it didn't work, what is the problem? my code :
<?php
$url= get_site_url();
$author_id = get_the_author_meta('ID');
$args = array(
'author' => $author_id,
'tax_query' => array(
'relation' => 'AND',
array(
'cat' => '161,181,157',
'post_count' => '3',
),
),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
$postid = get_the_ID();
echo '<a href='. $url .'?p='. $postid .'>' . get_the_title() . '</a>';
}
?>
Try the below code.
$url = get_site_url();
$author_id = get_the_author_meta('ID');
$args = array(
'author' => $author_id,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array(161,181,157)
),
),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
$postid = get_the_ID();
echo '<a href='. $url .'?p='. $postid .'>' . get_the_title() . '</a>';
}
I have a list of wordpress pages filtered by custom taxonomy. How can I turn this into wp function to use with shortcode?
Here is working filtration:
<?
$custom_terms = get_terms('csgroup');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'page', 'csgroup' => 'digital-expert-group',
// $args = array('post_type' => 'page', 'csgroup' => 'management-consultants-group',
'tax_query' => array(
array(
'taxonomy' => 'csgroup',
'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;
}
}
wp_reset_postdata();
?>
I want to get this:
function l1category_list_func(){
==my php code with list of pages==
}
add_shortcode( 'l1category_list', 'l1category_list_func' );
If I understand your question correctly, you want to use the return statement to return a concatenated string of the outputs that you were echoing before.
function l1category_list_func(){
$output = '';
$custom_terms = get_terms('csgroup');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'page', 'csgroup' => 'digital-expert-group',
// $args = array('post_type' => 'page', 'csgroup' => 'management-consultants-group',
'tax_query' => array(
array(
'taxonomy' => 'csgroup',
'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();
$output .= ''.get_the_title().'<br>';
endwhile;
}
}
wp_reset_postdata();
return $output;
}
I would like to display e.g. the title of a all posts with a custom post type and a specific taxonomy.
I tried following:
global $post;
$myposts = get_posts(array(
'post_type' => 'my-post-type',
'tax_query' => array(
array(
'taxonomy' => 'post-type-category',
'terms' => 'new-posts')
)
)
);
foreach ($myposts as $mypost) {
echo $mypost->post_title;
}
you can use WP Query :
<?php
$args = array(
'post_type' => 'my-post-type',
'tax_query' => array(
array(
'taxonomy' => 'post-type-category',
'field' => 'slug',
'terms' => 'new-posts',
),
),
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title() ; // title
the_excerpt(); // excerpt
the_post_thumbnail(); // post thumbnail
}
wp_reset_postdata();
} else {
// no posts found
}