skip to Main Content

im trying to creating a shortcode for cpt and custom taxonomy to list post titles

im using this codes to list the post titles:

function kat_post_list_func($atts)
{
    $category_id = $atts['cat'];
    $args = array('category' => $category_id, 'post_type' => 'post');
    $cat_posts = get_posts($args);
 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $posts_per_page = 2;
    $offset = ($paged - 1) * $posts_per_page;
    $total_pages = ceil(count($cat_posts) / $posts_per_page);
 
    $args['numberposts'] = $posts_per_page;
    $args['offset'] = $offset;
    $cat_posts = get_posts($args);
 
    $tablo = "<table>";
    $tablo .= "<tr>";
    $tablo .= "<th>Başlık</th>";
    $tablo .= "<th>Yazar</th>";
    $tablo .= "</tr>";
 
    foreach ($cat_posts as $post) {
        $author_name = get_the_author_meta('display_name', $post->post_author);
        $tablo .= "<tr>";
        $tablo .= "<td><a href='" . get_permalink($post->ID) . "'>" . $post->post_title . "</a></td>";
        $tablo .= "<td>" . $author_name . "</td>";
        $tablo .= "</tr>";
    }
 
    $tablo .= "</table>";
 
    $pagination = paginate_links(array(
        'base' => get_pagenum_link(1) . '%_%',
        'format' => '/page/%#%',
        'current' => $paged,
        'total' => $total_pages,
        'prev_text' => __('« Önceki'),
        'next_text' => __('Sonraki »'),
    ));
 
    $output = '<div class="pagination">' . $pagination . '</div>';
 
    return $tablo . $output;
}
 
add_shortcode('kat_post_list', 'kat_post_list_func');

im using the shortcode likes this: [kat_post_list cat=3]

but now i need something different. i have a cpt called demo_listing and a custom taxonomy under that cpt called demo_8…

i want to list post titles for this cpt also but i can’t figure out how: this is what i tried so far:

function kat_post_list_func($atts)
{
 
    $category_id = $atts['cat'];
    $args = array(
        'post_type' => 'demo_listing',
        'tax_query' => array(
            array(
                'taxonomy' => 'demo_8',
                'field' => 'term_id',
                'terms' => $category_id
            )
        )
    );
    $cat_posts = get_posts($args);

can someone help me with that and tell me where i am wrong please?

Update: okay so this is what i can’t figure out:

in the first codes this fecth the categories of the post post-type -> $category_id = $atts['cat']; but how can i get the terms for demo_8. demo_8 works like categories for posts.

UPDATE 2:

Now i changed my args:
but that codes print every post under the demo_listing cpt. i still can’t let the my taxonomy involved. but i think im getting closer lol

$terms = get_terms( array(
    'taxonomy' => 'demo_8',
    'fields'   => 'id=>slug',
) );
    
$args = array( 
    'post_type'      => 'demo_listing',
    'post_status'    => 'publish',
    'tax_query'      => array(
        array(
            'taxonomy' => 'demo_8',
            'field'    => 'term_id',
            'terms'    => array_keys( $terms ),
        ),
    ),
);
    $cat_posts = get_posts($args);

2

Answers


  1. function kat_post_list_func($atts) {
        $category_id = $atts['cat'];
        
        $args = array(
            'post_type' => 'demo_listing',
            'tax_query' => array(
                array(
                    'taxonomy' => 'demo_8',
                    'field' => 'term_id',
                    'terms' => $category_id
                )
            )
        );
        
        $cat_posts = get_posts($args);
     
        $tablo = "<table>";
        $tablo .= "<tr>";
        $tablo .= "<th>Başlık</th>";
        $tablo .= "<th>Yazar</th>";
        $tablo .= "</tr>";
     
        foreach ($cat_posts as $post) {
            $author_name = get_the_author_meta('display_name', $post->post_author);
            $tablo .= "<tr>";
            $tablo .= "<td><a href='" . get_permalink($post->ID) . "'>" . $post->post_title . "</a></td>";
            $tablo .= "<td>" . $author_name . "</td>";
            $tablo .= "</tr>";
        }
     
        $tablo .= "</table>";
     
        return $tablo;
    }
     
    add_shortcode('kat_post_list', 'kat_post_list_func');
    
    Login or Signup to reply.
  2. Here’s how I would write the shortcode function (untested):

    function kat_post_list_func( $atts ) {
        $category_id = $atts['cat'];
    
        if ( empty( $category_id ) ) {
            trigger_error( 'No category provided.', E_USER_NOTICE );
            return;
        }
    
        $args = array( 
            'post_type'      => 'demo_listing',
            'post_status'    => 'publish',
            'no_found_rows'  => true,
            'tax_query'      => array(
                array(
                    'taxonomy' => 'demo_8',
                    'field'    => 'term_id',
                    'terms'    => $category_id,
                ),
            ),
        );
    
        $cat_posts = new WP_Query( $args );
        
        if ( empty( $cat_posts->posts ) ) {
            echo 'No posts found.';
            return;
        }
    
        ...
    }
    

    If you’re not getting results from this, try dumping $cat_posts->query_vars to check if there’s any manipulation of the query parameters occurring.

    $cat_posts = new WP_Query( $args );
    var_dump( $cat_posts->query_vars );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search