1

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 class="gmb_arrow-icon"><?xml version="1.0" encoding="iso-8859-1"?><svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve"><g><g><path d="M506.134,241.843c-0.006-0.006-0.011-0.013-0.018-0.019l-104.504-104c-7.829-7.791-20.492-7.762-28.285,0.068 c-7.792,7.829-7.762,20.492,0.067,28.284L443.558,236H20c-11.046,0-20,8.954-20,20c0,11.046,8.954,20,20,20h423.557           l-70.162,69.824c-7.829,7.792-7.859,20.455-0.067,28.284c7.793,7.831,20.457,7.858,28.285,0.068l104.504-104 c0.006-0.006,0.011-0.013,0.018-0.019C513.968,262.339,513.943,249.635,506.134,241.843z"/></g></g></svg></div></div></article></a>';
    }

wp_reset_postdata();

$output .= '</div>';

endif;

return $output;

}
// Register shortcode
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

1

You're sure that you have actual posts (project post type), that have that category selected on them, right?

Make sure the posts have an actual category selected, not a tag. Also check in the categories configuration to make sure that websites is the actual slug of the category they are assigned to.

You can also try using the actual category id (find it while editing the category in admin area, in the URL bar), using this instead: 'cat' => '1,2,3,4'

or try 'category__in' => 4

I suspect the issue is probably with incorrect slug or some other configuration

https://developer.wordpress.org/reference/classes/wp_query/#category-parameters

You can also try using a direct tax query:

$args = array(
    'post_type'     => 'project',
    'tax_query' => array(
        array(
            'taxonomy'         => 'category',
            'terms'            => 'websites',
            'field'            => 'slug',
            'include_children' => true,
        )
    )
);
5
  • Yes I have checked it is the correct post type, category name (slug) - I did even try using the category ID, and same outcome.. :/ (or rather, no outcome) - I'll give it another go though. – benelwoods Jan 31 at 22:35
  • I am fairly new to wordpress development - could there be any conflicts from other similar functions? (e.g. I have another short code function which has get_categories($args); - however it each of these functions have wp_reset_postdata(); towards the end. – benelwoods Jan 31 at 22:43
  • Possibly, or something else could be filtering on it as well (plugin, theme, etc). I updated the code with a direct tax query you can try – mtarkenton Jan 31 at 22:45
  • Just gave tax_query a go, still not working ahah! - It MUST be some other sort of conflict if the syntax is correct, hmmm. – benelwoods Jan 31 at 22:51
  • The slug for the project category page is project_category/websites - is that normal? – benelwoods Jan 31 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.