Question

The code below displays posts that have matching 'page name' and 'category slug'.. but I am trying to get it to use a custom taxonomy. How do I incorporate that into the below? I'm using this in my function file:

function my_get_posts( $category_name )
   {
    // set the criteria 
    $args = array(
        'numberposts' => -1,
        'category_name' => $category_name,
        'post_type' => 'custom_type'
    );
    // return the object array of the posts.
    return get_posts( $args );
}

and this in my page

<?php
    $posts_returned = my_get_posts($post->post_name);

    foreach ($posts_returned as $post_returned) {

    $postlink = get_post_permalink($post_returned->ID);

?>

HTML HERE

<?php   } ?>
Was it helpful?

Solution

Worked it out

function sherwood_get_posts( $category_name )
{
    // set the criteria 
    $args = array(
        'numberposts' => -1,
        'post_type' => 'custom_type',
        'tax_query' => array(
                array(
                'taxonomy' => 'custom_cat', 
                'field' => 'slug', 
                'terms' => array($category_name))
            )       
    );
    // return the object array of the posts.
    return get_posts( $args );
}

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top