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;
}
Related
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'm trying to create a custom easy digital downloads (EDD) shortcode and I can't make the atts working. Meaning I would like to be able to change the category and tag names while using the shortcode, e.g :
<?php echo do_shortcode( '[my_shortcode taxonomy="download_tag" field="name" terms="Premium"]' ); ?>
But all I get is the default term ( Free ).
The code:
function my_shortcode_function($product_args, $content = null) {
global $post;
$current_page = get_query_var('paged');
$offset = $current_page > 0 ? $per_page * ($current_page-1) : 0;
$product_args = array(
'post_type' => 'download',
'posts_per_page' => '6',
'tax_query' => array(
array(
'taxonomy' => 'download_tag',
'field' => 'name',
'terms' => 'Free'
),
),
'offset' => $offset
);
$products = new WP_Query($product_args);
if ($products->have_posts()) : $i = 1;
while ($products->have_posts()) : $products->the_post();
$tags = get_the_term_list( get_the_ID(), 'download_tag' );// get tags link
$tags = strip_tags( $tags ); // get rid of the tag link
$category = get_the_term_list( get_the_ID(), 'download_category' ); // get category link
echo '<div class="col-md-4 col-lg-4">';
get_template_part( 'template-parts/content-download-card-post' );
echo '</div>';
$i+=1;
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
}
add_shortcode( 'my_shortcode', 'my_shortcode_function' );
I'm sure there are better ways to do it but I'm still new in php and the EDD docs lacks of useful examples for newbies like me. So I'll appreciate any help. Thanks a lot.
It's funny that I implemented this from a woocommerce answer and it works like a charm. Now I can change the tag, category, or the number of posts. If anyone needs it here it is:
// Creating a shortcode that displays a random product image/thumbail
if( !function_exists('prod_listing_params') ) {
function prod_listing_params( $atts ) {
ob_start();
$atts = shortcode_atts( array (
'type' => 'download',
'order' => 'date',
'orderby' => 'title',
'posts' => 6,
'category' => '', // category name
'tag' => '', // tag name
), $atts, 'list_products' );
$query = new WP_Query( array(
'post_type' => $atts['type'],
'order' => $atts['order'],
'orderby' => $atts['orderby'],
'posts_per_page' => $atts['posts'],
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'download_category',
'field' => 'name',
'terms' => $atts['category'],
),
array(
'taxonomy' => 'download_tag',
'field' => 'name',
'terms' => $atts['tag'],
)
),
) );
if ( $query->have_posts() ) { $i = 1;
?>
<?php while ( $query->have_posts() ) : $query->the_post();
$tags = get_the_term_list( get_the_ID(), 'download_tag' );// get tags link
$tags = strip_tags( $tags ); // get rid of the tag link
$category = get_the_term_list( get_the_ID(), 'download_category' ); // get category link
?>
<?php echo '<div class="col-md-4 col-lg-4">';
get_template_part( 'template-parts/content-download-card-post' );
echo '</div>'; $i+=1; ?>
<?php endwhile;
wp_reset_postdata(); ?>
<?php
$myvar = ob_get_clean();
return $myvar;
}
}
add_shortcode( 'list_products', 'prod_listing_params' );
And the use:
<?php echo do_shortcode( '[list_products tag="Free" posts="3"]' ); ?>
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
}
I'm trying to overwrite the default 'Blog pages show at most' which is set to 5 posts. I have a custom post type called 'FAQs', which has the argument 'posts_per_page' => 999 in the query for getting all the posts of this type, however I can't seem to override the default restriction in the WordPress setting. My code for the FAQ query is below, which works on my local machine (MAMP) but not when I upload it to live. how do I show all posts of that type?
<?php
wp_reset_query();
// Query for getting custom taxonomy 'FAQ Type' of custom post type 'FAQs'
$cat_args = array (
'taxonomy' => 'faq_type',
'exclude' => array(12),
'posts_per_page' => 999,
//'show_all' => true,
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_categories ( $cat_args );
foreach ( $categories as $category ) {
//wp_reset_query();
$cat_query = null;
// Query for getting posts of custom post type 'FAQs'
$args = array (
'post_type' => 'faq',
'faq_type' => $category->slug,
'posts_per_page' => 999,
//'show_all' => true,
'orderby' => 'simple_page_ordering_is_sortable',
);
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) { ?>
<?php echo "<h2>". $category->name ."</h2>"; ?>
<ul id="resident-accordion" class="accordion white-bg-accordion" data-accordion data-allow-all-closed="true" role="tablist">
<?php
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
?>
<li class="accordion-item faq-content <?php //if ($firstLoop === true) { echo "is-active"; }?>" data-accordion-item>
<?php the_title(); ?>
<div class="accordion-content" data-tab-content>
<?php the_content(); ?>
</div>
</li>
<?php
} //wp_reset_query();
wp_reset_postdata(); //End WHILE
echo "</ul>";
} //End IF
wp_reset_postdata();
//wp_reset_query();
} //End FOR
?>
You can try to use this code below :
<?php
$cat_args = array(
'taxonomy' => 'faq_type',
'exclude' => array(7),
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_terms( $cat_args );
foreach ( $categories as $category )
{
$args = array(
'post_type' => 'faq',
'posts_per_page' => -1, // load all posts
'orderby' => 'simple_page_ordering_is_sortable',
'tax_query' => array(
array(
'taxonomy' => 'faq_type',
'field' => 'slug',
'terms' => $category->slug
)
)
);
$cat_query = new WP_Query( $args );
// enter the rest of your code below
}
Or you can use get_posts() to receive posts list.
<?php
$cat_args = array(
'taxonomy' => 'faq_type',
'exclude' => array(7),
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_terms( $cat_args );
foreach ( $categories as $category )
{
$posts = get_posts(array(
'numberposts' => -1, // get all posts.
'tax_query' => array(
array(
'taxonomy' => 'faq_type',
'field' => 'slug',
'terms' => $category->slug,
'operator' => 'IN',
),
),
'post_type' => 'faq',
));
// enter the rest of your code below
}
Cheers!
You found your answer by now I hope, but you were so close.
Instead of:
'posts_per_page' => 999,
try:
'posts_per_page' => -1,
see: Pagination Parameters
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,
),
),
);