skip to Main Content

I am attempting to simply pull in all projects in a specific category (e.g. ‘websites’ or ‘featured’).

When using:

$args = array(
    'post_type'     => 'project',
    'category_name'  => 'websites',
    'posts_per_page' => 10,
);

Nothing is returned.

When using simply:

$args = array(
    'post_type'     => 'project',
);

I do actually get posts (projects) returning, just not in the category I want.

Here is my full code:

function gmb_register_project_section () { 

$args = array(
    'post_type'     => 'project',
    'category_name'  => 'websites',
    'posts_per_page' => 10,
);

$post_query = new WP_Query($args);

if ($post_query->have_posts()) :
$output = '<div class="gmb_custom-project-module">';

    while ($post_query->have_posts()) {
        $post_query->the_post();

        $output .= '<a href="'. get_permalink(). '"><article class="gmb_project-item" style="background: url(''. get_the_post_thumbnail_url(). '') no-repeat center center;"><div class="inner"><h2>'.
                get_the_title(). '</h2></div></article></a>';
    }

wp_reset_postdata();

$output .= '</div>';

endif;

return $output; }

add_shortcode('gmb_project_section', 'gmb_register_project_section');

I am using the Divi theme, with a code module which allows you to input short codes into.

Thanks in advance,
Ben

2

Answers


  1. Try this bro:

    $args = array(
    'post_type'     => 'project',
    'posts_per_page' => 10,
    'tax_query' => array(
                    array(
                        'taxonomy' => 'categories',
                        'field' => 'slug',
                        'terms' => 'websites',
                    ),
                ),
     );
    
    Login or Signup to reply.
  2. function gmb_register_project_section () { 
    
    $args = array(
        'post_type'     => 'project',
        'posts_per_page' => 10,
         'tax_query' => array(
                array(
                    'taxonomy' => 'your_taxonomy_name',  //Add your post type's taxonomy name
                    'field' => 'slug',
                    'terms' => 'websites',
                ),
            ),
    );
    
    $post_query = new WP_Query($args);
    
    if ($post_query->have_posts()) :
    $output = '<div class="gmb_custom-project-module">';
    
        while ($post_query->have_posts()) {
            $post_query->the_post();
    
            $output .= '<a href="'. get_permalink(). '"><article class="gmb_project-item" style="background: url(''. get_the_post_thumbnail_url(). '') no-repeat center center;"><div class="inner"><h2>'.
                    get_the_title(). '</h2></div></article></a>';
        }
    
    wp_reset_postdata();
    
    $output .= '</div>';
    
    endif;
    
    return $output; }
    
    add_shortcode('gmb_project_section', 'gmb_register_project_section');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search