Actually the problem is in this code that is, If I put the id directly in "terms" of any post type like; [arts-list 10]
, then this type of post which contains this id displays the posts on the frontend but if I put something else like terms => 'type'
and then I try to display a specific type posts like; [arts-list type=10]
, no post displays on the frontend, actually in terms I think I have to pass parameter of shortcode but how I do, I can't debug this, that's why I asked you. I hope now you understand what is my problem.
function diwp_create_shortcode_arts_post_type(){
$atts = shortcode_atts( array(
'type' => ' ',
), $atts );
$args = array(
'post_type' => 'arts',
'posts_per_page' => '10',
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'Types',
'field' => 'term_id',
'terms' => '10',
) )
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$result .= '<div class="art-item">';
$result .= '<div class="art-image">' . get_the_post_thumbnail() . '</div>';
$result .= '<div class="art-name">' . get_the_title() . '</div>';
$result .= '<div class="art-desc">' . get_the_content() . '</div>';
$result .= '</div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode( 'arts-list', 'diwp_create_shortcode_arts_post_type' );
Actually the problem is in this code that is, If I put the id directly in "terms" of any post type like; [arts-list 10]
, then this type of post which contains this id displays the posts on the frontend but if I put something else like terms => 'type'
and then I try to display a specific type posts like; [arts-list type=10]
, no post displays on the frontend, actually in terms I think I have to pass parameter of shortcode but how I do, I can't debug this, that's why I asked you. I hope now you understand what is my problem.
function diwp_create_shortcode_arts_post_type(){
$atts = shortcode_atts( array(
'type' => ' ',
), $atts );
$args = array(
'post_type' => 'arts',
'posts_per_page' => '10',
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'Types',
'field' => 'term_id',
'terms' => '10',
) )
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) :
$query->the_post() ;
$result .= '<div class="art-item">';
$result .= '<div class="art-image">' . get_the_post_thumbnail() . '</div>';
$result .= '<div class="art-name">' . get_the_title() . '</div>';
$result .= '<div class="art-desc">' . get_the_content() . '</div>';
$result .= '</div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode( 'arts-list', 'diwp_create_shortcode_arts_post_type' );
Share
Improve this question
edited Jan 12, 2021 at 6:06
fuxia♦
107k38 gold badges255 silver badges459 bronze badges
asked Jan 12, 2021 at 5:39
Moiz AslamMoiz Aslam
34 bronze badges
0
1 Answer
Reset to default 0I think I have to pass parameter of shortcode, but how I do
Yes, that's correct, and you already are passing the parameter to your shortcode when you do [arts-list type=10]
— although you should enclose the parameter value in quotes, i.e. type="10"
.
But the $atts
in your shortcode function is not defined before the shortcode_atts()
is called, so the function should be defined as follows:
function diwp_create_shortcode_arts_post_type( $atts )
And because the function is already parsing the parameters which are stored in the $atts
array, then you just need to use the parameters in your tax_query
(i.e. taxonomy query) clause:
'tax_query' => array(
array(
'taxonomy' => 'Types',
'field' => 'term_id',
'terms' => wp_parse_id_list( $atts['type'] ),
),
),
Note: In the above example, I used wp_parse_id_list()
so that we can pass a list of term IDs, e.g. [arts-list type="10,11,12"]
. But more importantly, make sure the IDs point to valid terms in the Types
taxonomy.