How to display posts based on sub categories of custom post type?












1















I have a custom post type called "categories" and it's slug is "category". I have various categories like:




  • Beauty


    • Makeup

    • Skincare



  • Tech


I have these categories in a CPT called "Products". I want to display only those posts that match with a subcategory. For example, I want to display only those posts from CPT "Products" that have "Makeup" checked in categories custom taxonomy. I have tried the following code:



$args= new WP_Query( array(
'post_type' => 'Products',
'tax_query' => array(
array (
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => 'Beauty',
)
),
) );
if($args->have_posts()):
while ($args->have_posts()):$args->the_post();
echo get_field('name');
endwhile;
endif;


But this code obviously displays posts that have category checked as "Beauty". It doesn't check for subcategory. Can anyone help me out with this? Any modification to the current code would be helpful as well.
Thanks!










share|improve this question

























  • is this woocommerce products ?

    – Angel Deykov
    Nov 28 '18 at 6:49











  • No. Products is just custom post type. The data fed into this cpt comes from a form on the front end.

    – Komal R
    Nov 28 '18 at 6:53
















1















I have a custom post type called "categories" and it's slug is "category". I have various categories like:




  • Beauty


    • Makeup

    • Skincare



  • Tech


I have these categories in a CPT called "Products". I want to display only those posts that match with a subcategory. For example, I want to display only those posts from CPT "Products" that have "Makeup" checked in categories custom taxonomy. I have tried the following code:



$args= new WP_Query( array(
'post_type' => 'Products',
'tax_query' => array(
array (
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => 'Beauty',
)
),
) );
if($args->have_posts()):
while ($args->have_posts()):$args->the_post();
echo get_field('name');
endwhile;
endif;


But this code obviously displays posts that have category checked as "Beauty". It doesn't check for subcategory. Can anyone help me out with this? Any modification to the current code would be helpful as well.
Thanks!










share|improve this question

























  • is this woocommerce products ?

    – Angel Deykov
    Nov 28 '18 at 6:49











  • No. Products is just custom post type. The data fed into this cpt comes from a form on the front end.

    – Komal R
    Nov 28 '18 at 6:53














1












1








1


1






I have a custom post type called "categories" and it's slug is "category". I have various categories like:




  • Beauty


    • Makeup

    • Skincare



  • Tech


I have these categories in a CPT called "Products". I want to display only those posts that match with a subcategory. For example, I want to display only those posts from CPT "Products" that have "Makeup" checked in categories custom taxonomy. I have tried the following code:



$args= new WP_Query( array(
'post_type' => 'Products',
'tax_query' => array(
array (
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => 'Beauty',
)
),
) );
if($args->have_posts()):
while ($args->have_posts()):$args->the_post();
echo get_field('name');
endwhile;
endif;


But this code obviously displays posts that have category checked as "Beauty". It doesn't check for subcategory. Can anyone help me out with this? Any modification to the current code would be helpful as well.
Thanks!










share|improve this question
















I have a custom post type called "categories" and it's slug is "category". I have various categories like:




  • Beauty


    • Makeup

    • Skincare



  • Tech


I have these categories in a CPT called "Products". I want to display only those posts that match with a subcategory. For example, I want to display only those posts from CPT "Products" that have "Makeup" checked in categories custom taxonomy. I have tried the following code:



$args= new WP_Query( array(
'post_type' => 'Products',
'tax_query' => array(
array (
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => 'Beauty',
)
),
) );
if($args->have_posts()):
while ($args->have_posts()):$args->the_post();
echo get_field('name');
endwhile;
endif;


But this code obviously displays posts that have category checked as "Beauty". It doesn't check for subcategory. Can anyone help me out with this? Any modification to the current code would be helpful as well.
Thanks!







php wordpress custom-post-type custom-taxonomy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 7:01







Komal R

















asked Nov 28 '18 at 6:16









Komal RKomal R

949




949













  • is this woocommerce products ?

    – Angel Deykov
    Nov 28 '18 at 6:49











  • No. Products is just custom post type. The data fed into this cpt comes from a form on the front end.

    – Komal R
    Nov 28 '18 at 6:53



















  • is this woocommerce products ?

    – Angel Deykov
    Nov 28 '18 at 6:49











  • No. Products is just custom post type. The data fed into this cpt comes from a form on the front end.

    – Komal R
    Nov 28 '18 at 6:53

















is this woocommerce products ?

– Angel Deykov
Nov 28 '18 at 6:49





is this woocommerce products ?

– Angel Deykov
Nov 28 '18 at 6:49













No. Products is just custom post type. The data fed into this cpt comes from a form on the front end.

– Komal R
Nov 28 '18 at 6:53





No. Products is just custom post type. The data fed into this cpt comes from a form on the front end.

– Komal R
Nov 28 '18 at 6:53












1 Answer
1






active

oldest

votes


















1














If you want to display posts from a certain subcategory you can simply use get_posts(), something like:



 $posts = get_posts(array(
'post_type' => 'Products',
'post_status' => 'publish',
'cat' => your subcat ID,
'posts_per_page' => -1
));


And then loop through your posts like:



 foreach ($posts as $post){
echo $post->post_title . '<br>';

}





share|improve this answer
























  • no need to over-complicate this, as far I understood you want posts from 1 certain subcategory, if this is not the case then tell me I'll edit the answer

    – Angel Deykov
    Nov 28 '18 at 7:27











  • You understood correctly. However, this displays all posts regardless of subcategory. How do I get the id of sub category?

    – Komal R
    Nov 28 '18 at 7:31













  • wpwhitesecurity.com/find-wordpress-category-id

    – Angel Deykov
    Nov 28 '18 at 7:36











  • Is there any way to do it dynamically so that it can be done even if client adds new subcategory?

    – Komal R
    Nov 28 '18 at 7:48











  • Anything can be done yes, but you need to tell me what's exactly your user case scenario. What is it that you're trying to achieve and why archive.php template is not doing the job ? I mean when a user clicks on some of your subcats link anyways he sees posts only from this category displayed in the archive templayed.

    – Angel Deykov
    Nov 28 '18 at 7:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513235%2fhow-to-display-posts-based-on-sub-categories-of-custom-post-type%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If you want to display posts from a certain subcategory you can simply use get_posts(), something like:



 $posts = get_posts(array(
'post_type' => 'Products',
'post_status' => 'publish',
'cat' => your subcat ID,
'posts_per_page' => -1
));


And then loop through your posts like:



 foreach ($posts as $post){
echo $post->post_title . '<br>';

}





share|improve this answer
























  • no need to over-complicate this, as far I understood you want posts from 1 certain subcategory, if this is not the case then tell me I'll edit the answer

    – Angel Deykov
    Nov 28 '18 at 7:27











  • You understood correctly. However, this displays all posts regardless of subcategory. How do I get the id of sub category?

    – Komal R
    Nov 28 '18 at 7:31













  • wpwhitesecurity.com/find-wordpress-category-id

    – Angel Deykov
    Nov 28 '18 at 7:36











  • Is there any way to do it dynamically so that it can be done even if client adds new subcategory?

    – Komal R
    Nov 28 '18 at 7:48











  • Anything can be done yes, but you need to tell me what's exactly your user case scenario. What is it that you're trying to achieve and why archive.php template is not doing the job ? I mean when a user clicks on some of your subcats link anyways he sees posts only from this category displayed in the archive templayed.

    – Angel Deykov
    Nov 28 '18 at 7:50
















1














If you want to display posts from a certain subcategory you can simply use get_posts(), something like:



 $posts = get_posts(array(
'post_type' => 'Products',
'post_status' => 'publish',
'cat' => your subcat ID,
'posts_per_page' => -1
));


And then loop through your posts like:



 foreach ($posts as $post){
echo $post->post_title . '<br>';

}





share|improve this answer
























  • no need to over-complicate this, as far I understood you want posts from 1 certain subcategory, if this is not the case then tell me I'll edit the answer

    – Angel Deykov
    Nov 28 '18 at 7:27











  • You understood correctly. However, this displays all posts regardless of subcategory. How do I get the id of sub category?

    – Komal R
    Nov 28 '18 at 7:31













  • wpwhitesecurity.com/find-wordpress-category-id

    – Angel Deykov
    Nov 28 '18 at 7:36











  • Is there any way to do it dynamically so that it can be done even if client adds new subcategory?

    – Komal R
    Nov 28 '18 at 7:48











  • Anything can be done yes, but you need to tell me what's exactly your user case scenario. What is it that you're trying to achieve and why archive.php template is not doing the job ? I mean when a user clicks on some of your subcats link anyways he sees posts only from this category displayed in the archive templayed.

    – Angel Deykov
    Nov 28 '18 at 7:50














1












1








1







If you want to display posts from a certain subcategory you can simply use get_posts(), something like:



 $posts = get_posts(array(
'post_type' => 'Products',
'post_status' => 'publish',
'cat' => your subcat ID,
'posts_per_page' => -1
));


And then loop through your posts like:



 foreach ($posts as $post){
echo $post->post_title . '<br>';

}





share|improve this answer













If you want to display posts from a certain subcategory you can simply use get_posts(), something like:



 $posts = get_posts(array(
'post_type' => 'Products',
'post_status' => 'publish',
'cat' => your subcat ID,
'posts_per_page' => -1
));


And then loop through your posts like:



 foreach ($posts as $post){
echo $post->post_title . '<br>';

}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 7:23









Angel DeykovAngel Deykov

519313




519313













  • no need to over-complicate this, as far I understood you want posts from 1 certain subcategory, if this is not the case then tell me I'll edit the answer

    – Angel Deykov
    Nov 28 '18 at 7:27











  • You understood correctly. However, this displays all posts regardless of subcategory. How do I get the id of sub category?

    – Komal R
    Nov 28 '18 at 7:31













  • wpwhitesecurity.com/find-wordpress-category-id

    – Angel Deykov
    Nov 28 '18 at 7:36











  • Is there any way to do it dynamically so that it can be done even if client adds new subcategory?

    – Komal R
    Nov 28 '18 at 7:48











  • Anything can be done yes, but you need to tell me what's exactly your user case scenario. What is it that you're trying to achieve and why archive.php template is not doing the job ? I mean when a user clicks on some of your subcats link anyways he sees posts only from this category displayed in the archive templayed.

    – Angel Deykov
    Nov 28 '18 at 7:50



















  • no need to over-complicate this, as far I understood you want posts from 1 certain subcategory, if this is not the case then tell me I'll edit the answer

    – Angel Deykov
    Nov 28 '18 at 7:27











  • You understood correctly. However, this displays all posts regardless of subcategory. How do I get the id of sub category?

    – Komal R
    Nov 28 '18 at 7:31













  • wpwhitesecurity.com/find-wordpress-category-id

    – Angel Deykov
    Nov 28 '18 at 7:36











  • Is there any way to do it dynamically so that it can be done even if client adds new subcategory?

    – Komal R
    Nov 28 '18 at 7:48











  • Anything can be done yes, but you need to tell me what's exactly your user case scenario. What is it that you're trying to achieve and why archive.php template is not doing the job ? I mean when a user clicks on some of your subcats link anyways he sees posts only from this category displayed in the archive templayed.

    – Angel Deykov
    Nov 28 '18 at 7:50

















no need to over-complicate this, as far I understood you want posts from 1 certain subcategory, if this is not the case then tell me I'll edit the answer

– Angel Deykov
Nov 28 '18 at 7:27





no need to over-complicate this, as far I understood you want posts from 1 certain subcategory, if this is not the case then tell me I'll edit the answer

– Angel Deykov
Nov 28 '18 at 7:27













You understood correctly. However, this displays all posts regardless of subcategory. How do I get the id of sub category?

– Komal R
Nov 28 '18 at 7:31







You understood correctly. However, this displays all posts regardless of subcategory. How do I get the id of sub category?

– Komal R
Nov 28 '18 at 7:31















wpwhitesecurity.com/find-wordpress-category-id

– Angel Deykov
Nov 28 '18 at 7:36





wpwhitesecurity.com/find-wordpress-category-id

– Angel Deykov
Nov 28 '18 at 7:36













Is there any way to do it dynamically so that it can be done even if client adds new subcategory?

– Komal R
Nov 28 '18 at 7:48





Is there any way to do it dynamically so that it can be done even if client adds new subcategory?

– Komal R
Nov 28 '18 at 7:48













Anything can be done yes, but you need to tell me what's exactly your user case scenario. What is it that you're trying to achieve and why archive.php template is not doing the job ? I mean when a user clicks on some of your subcats link anyways he sees posts only from this category displayed in the archive templayed.

– Angel Deykov
Nov 28 '18 at 7:50





Anything can be done yes, but you need to tell me what's exactly your user case scenario. What is it that you're trying to achieve and why archive.php template is not doing the job ? I mean when a user clicks on some of your subcats link anyways he sees posts only from this category displayed in the archive templayed.

– Angel Deykov
Nov 28 '18 at 7:50




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53513235%2fhow-to-display-posts-based-on-sub-categories-of-custom-post-type%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Futebolista

Jornalista