Not able to fetch posts using CPT categories
I have registered a CPT in WordPress theme. I can able to fetch posts in the loop by post_type but when I am trying to get posts by category_name. It is not giving me results.
add_action( 'init', 'achivement', 0 );
function achivement() {
register_post_type( 'achivement', array(
'label' => __( 'Achivement', 'achivement-free' ),
'description' => __( 'Achivement custom post type.', 'achivement-free' ),
'public' => false,
'has_archive' => false,
'publicaly_queryable' => false,
'query_var' => false,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-networking',
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'hierarchical' => true,
'menu_position' => 20,
'supports' => array(
'title',
'editor',
),
'capability_type' => 'post',
'labels' => array(
'name' => __( 'Achivements', 'achivement-free' ),
'singular_name' => __( 'Achivement', 'achivement-free' ),
'menu_name' => __( 'Achivement', 'achivement-free' ),
'all_items' => __( 'Achivements', 'achivement-free' ),
'add_new' => __( 'Add Achivement', 'achivement-free' ),
'add_new_item' => __( 'Add Achivement', 'achivement-free' ),
'edit' => __( 'Edit', 'achivement-free' ),
'edit_item' => __( 'Edit Achivement', 'achivement-free' ),
'new_item' => __( 'New Achivement', 'achivement-free' ),
'search_items' => __( 'Search Achivements', 'achivement-free' ),
'not_found' => __( 'No Achivements found', 'achivement-free' ),
'not_found_in_trash' => __( 'No Achivements found in Trash', 'achivement-free' ),
'parent' => __( 'Parent Achivements', 'achivement-free' ),
)
) );
register_taxonomy(
'achivement-category',
'achivement',
array(
'label' => __( 'Category' ),
'rewrite' => array( 'slug' => 'achivement-category' ),
'hierarchical' => true,
)
);
}
and here is the loop which I am writing to fetch the posts by category_name in WordPress.
$posts = get_posts(array(
'post_status' => 'publish',
'posts_per_page' => -1,
'post_type' => 'achivement',
'order' => 'ASC',
));
if( $posts ):
foreach( $posts as $post ):
setup_postdata( $post );
var_dump(get_the_category());
endforeach;
wp_reset_postdata();
endif;
I know maybe there will be a silly minor mistake but I am not able to get it. Please let me know if you can find that mistake. I will be very thankful.
Thanks
Got a working query from below answer. I am pasting that here for others.
$args = array(
'post_type' => 'achivement',
'posts_per_page' => 500,
'tax_query' => array(
array(
'taxonomy' => 'strike',
'field' => 'slug',
'terms' => array( 'strike-1' ),
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
echo get_the_title().'<br />';
endwhile;
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
wordpress wordpress-theming
add a comment |
I have registered a CPT in WordPress theme. I can able to fetch posts in the loop by post_type but when I am trying to get posts by category_name. It is not giving me results.
add_action( 'init', 'achivement', 0 );
function achivement() {
register_post_type( 'achivement', array(
'label' => __( 'Achivement', 'achivement-free' ),
'description' => __( 'Achivement custom post type.', 'achivement-free' ),
'public' => false,
'has_archive' => false,
'publicaly_queryable' => false,
'query_var' => false,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-networking',
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'hierarchical' => true,
'menu_position' => 20,
'supports' => array(
'title',
'editor',
),
'capability_type' => 'post',
'labels' => array(
'name' => __( 'Achivements', 'achivement-free' ),
'singular_name' => __( 'Achivement', 'achivement-free' ),
'menu_name' => __( 'Achivement', 'achivement-free' ),
'all_items' => __( 'Achivements', 'achivement-free' ),
'add_new' => __( 'Add Achivement', 'achivement-free' ),
'add_new_item' => __( 'Add Achivement', 'achivement-free' ),
'edit' => __( 'Edit', 'achivement-free' ),
'edit_item' => __( 'Edit Achivement', 'achivement-free' ),
'new_item' => __( 'New Achivement', 'achivement-free' ),
'search_items' => __( 'Search Achivements', 'achivement-free' ),
'not_found' => __( 'No Achivements found', 'achivement-free' ),
'not_found_in_trash' => __( 'No Achivements found in Trash', 'achivement-free' ),
'parent' => __( 'Parent Achivements', 'achivement-free' ),
)
) );
register_taxonomy(
'achivement-category',
'achivement',
array(
'label' => __( 'Category' ),
'rewrite' => array( 'slug' => 'achivement-category' ),
'hierarchical' => true,
)
);
}
and here is the loop which I am writing to fetch the posts by category_name in WordPress.
$posts = get_posts(array(
'post_status' => 'publish',
'posts_per_page' => -1,
'post_type' => 'achivement',
'order' => 'ASC',
));
if( $posts ):
foreach( $posts as $post ):
setup_postdata( $post );
var_dump(get_the_category());
endforeach;
wp_reset_postdata();
endif;
I know maybe there will be a silly minor mistake but I am not able to get it. Please let me know if you can find that mistake. I will be very thankful.
Thanks
Got a working query from below answer. I am pasting that here for others.
$args = array(
'post_type' => 'achivement',
'posts_per_page' => 500,
'tax_query' => array(
array(
'taxonomy' => 'strike',
'field' => 'slug',
'terms' => array( 'strike-1' ),
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
echo get_the_title().'<br />';
endwhile;
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
wordpress wordpress-theming
add a comment |
I have registered a CPT in WordPress theme. I can able to fetch posts in the loop by post_type but when I am trying to get posts by category_name. It is not giving me results.
add_action( 'init', 'achivement', 0 );
function achivement() {
register_post_type( 'achivement', array(
'label' => __( 'Achivement', 'achivement-free' ),
'description' => __( 'Achivement custom post type.', 'achivement-free' ),
'public' => false,
'has_archive' => false,
'publicaly_queryable' => false,
'query_var' => false,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-networking',
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'hierarchical' => true,
'menu_position' => 20,
'supports' => array(
'title',
'editor',
),
'capability_type' => 'post',
'labels' => array(
'name' => __( 'Achivements', 'achivement-free' ),
'singular_name' => __( 'Achivement', 'achivement-free' ),
'menu_name' => __( 'Achivement', 'achivement-free' ),
'all_items' => __( 'Achivements', 'achivement-free' ),
'add_new' => __( 'Add Achivement', 'achivement-free' ),
'add_new_item' => __( 'Add Achivement', 'achivement-free' ),
'edit' => __( 'Edit', 'achivement-free' ),
'edit_item' => __( 'Edit Achivement', 'achivement-free' ),
'new_item' => __( 'New Achivement', 'achivement-free' ),
'search_items' => __( 'Search Achivements', 'achivement-free' ),
'not_found' => __( 'No Achivements found', 'achivement-free' ),
'not_found_in_trash' => __( 'No Achivements found in Trash', 'achivement-free' ),
'parent' => __( 'Parent Achivements', 'achivement-free' ),
)
) );
register_taxonomy(
'achivement-category',
'achivement',
array(
'label' => __( 'Category' ),
'rewrite' => array( 'slug' => 'achivement-category' ),
'hierarchical' => true,
)
);
}
and here is the loop which I am writing to fetch the posts by category_name in WordPress.
$posts = get_posts(array(
'post_status' => 'publish',
'posts_per_page' => -1,
'post_type' => 'achivement',
'order' => 'ASC',
));
if( $posts ):
foreach( $posts as $post ):
setup_postdata( $post );
var_dump(get_the_category());
endforeach;
wp_reset_postdata();
endif;
I know maybe there will be a silly minor mistake but I am not able to get it. Please let me know if you can find that mistake. I will be very thankful.
Thanks
Got a working query from below answer. I am pasting that here for others.
$args = array(
'post_type' => 'achivement',
'posts_per_page' => 500,
'tax_query' => array(
array(
'taxonomy' => 'strike',
'field' => 'slug',
'terms' => array( 'strike-1' ),
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
echo get_the_title().'<br />';
endwhile;
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
wordpress wordpress-theming
I have registered a CPT in WordPress theme. I can able to fetch posts in the loop by post_type but when I am trying to get posts by category_name. It is not giving me results.
add_action( 'init', 'achivement', 0 );
function achivement() {
register_post_type( 'achivement', array(
'label' => __( 'Achivement', 'achivement-free' ),
'description' => __( 'Achivement custom post type.', 'achivement-free' ),
'public' => false,
'has_archive' => false,
'publicaly_queryable' => false,
'query_var' => false,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-networking',
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'hierarchical' => true,
'menu_position' => 20,
'supports' => array(
'title',
'editor',
),
'capability_type' => 'post',
'labels' => array(
'name' => __( 'Achivements', 'achivement-free' ),
'singular_name' => __( 'Achivement', 'achivement-free' ),
'menu_name' => __( 'Achivement', 'achivement-free' ),
'all_items' => __( 'Achivements', 'achivement-free' ),
'add_new' => __( 'Add Achivement', 'achivement-free' ),
'add_new_item' => __( 'Add Achivement', 'achivement-free' ),
'edit' => __( 'Edit', 'achivement-free' ),
'edit_item' => __( 'Edit Achivement', 'achivement-free' ),
'new_item' => __( 'New Achivement', 'achivement-free' ),
'search_items' => __( 'Search Achivements', 'achivement-free' ),
'not_found' => __( 'No Achivements found', 'achivement-free' ),
'not_found_in_trash' => __( 'No Achivements found in Trash', 'achivement-free' ),
'parent' => __( 'Parent Achivements', 'achivement-free' ),
)
) );
register_taxonomy(
'achivement-category',
'achivement',
array(
'label' => __( 'Category' ),
'rewrite' => array( 'slug' => 'achivement-category' ),
'hierarchical' => true,
)
);
}
and here is the loop which I am writing to fetch the posts by category_name in WordPress.
$posts = get_posts(array(
'post_status' => 'publish',
'posts_per_page' => -1,
'post_type' => 'achivement',
'order' => 'ASC',
));
if( $posts ):
foreach( $posts as $post ):
setup_postdata( $post );
var_dump(get_the_category());
endforeach;
wp_reset_postdata();
endif;
I know maybe there will be a silly minor mistake but I am not able to get it. Please let me know if you can find that mistake. I will be very thankful.
Thanks
Got a working query from below answer. I am pasting that here for others.
$args = array(
'post_type' => 'achivement',
'posts_per_page' => 500,
'tax_query' => array(
array(
'taxonomy' => 'strike',
'field' => 'slug',
'terms' => array( 'strike-1' ),
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
echo get_the_title().'<br />';
endwhile;
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
wordpress wordpress-theming
wordpress wordpress-theming
edited Nov 25 '18 at 8:40
Qarar Ul Hassan
asked Nov 24 '18 at 9:15
Qarar Ul HassanQarar Ul Hassan
64111
64111
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
First of all, don't use 'posts_per_page' => -1,
, this has serious impact on the query speed.
Second of all, just use WP_Query
and specify the taxonomy parameter like described here: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
Something like this should work
$args = array(
'post_type' => 'achivement',
'posts_per_page' => 500,
'tax_query' => array(
array(
'taxonomy' => 'achivement-category',
'field' => 'name',
),
),
);
$query = new WP_Query( $args );
1
Thanks for your answer @dingo_d. Got it a bit late but it is working now. I am updating my query in question with the working query for others.
– Qarar Ul Hassan
Nov 25 '18 at 8:38
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456766%2fnot-able-to-fetch-posts-using-cpt-categories%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
First of all, don't use 'posts_per_page' => -1,
, this has serious impact on the query speed.
Second of all, just use WP_Query
and specify the taxonomy parameter like described here: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
Something like this should work
$args = array(
'post_type' => 'achivement',
'posts_per_page' => 500,
'tax_query' => array(
array(
'taxonomy' => 'achivement-category',
'field' => 'name',
),
),
);
$query = new WP_Query( $args );
1
Thanks for your answer @dingo_d. Got it a bit late but it is working now. I am updating my query in question with the working query for others.
– Qarar Ul Hassan
Nov 25 '18 at 8:38
add a comment |
First of all, don't use 'posts_per_page' => -1,
, this has serious impact on the query speed.
Second of all, just use WP_Query
and specify the taxonomy parameter like described here: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
Something like this should work
$args = array(
'post_type' => 'achivement',
'posts_per_page' => 500,
'tax_query' => array(
array(
'taxonomy' => 'achivement-category',
'field' => 'name',
),
),
);
$query = new WP_Query( $args );
1
Thanks for your answer @dingo_d. Got it a bit late but it is working now. I am updating my query in question with the working query for others.
– Qarar Ul Hassan
Nov 25 '18 at 8:38
add a comment |
First of all, don't use 'posts_per_page' => -1,
, this has serious impact on the query speed.
Second of all, just use WP_Query
and specify the taxonomy parameter like described here: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
Something like this should work
$args = array(
'post_type' => 'achivement',
'posts_per_page' => 500,
'tax_query' => array(
array(
'taxonomy' => 'achivement-category',
'field' => 'name',
),
),
);
$query = new WP_Query( $args );
First of all, don't use 'posts_per_page' => -1,
, this has serious impact on the query speed.
Second of all, just use WP_Query
and specify the taxonomy parameter like described here: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters
Something like this should work
$args = array(
'post_type' => 'achivement',
'posts_per_page' => 500,
'tax_query' => array(
array(
'taxonomy' => 'achivement-category',
'field' => 'name',
),
),
);
$query = new WP_Query( $args );
answered Nov 24 '18 at 14:05
dingo_ddingo_d
6,11194782
6,11194782
1
Thanks for your answer @dingo_d. Got it a bit late but it is working now. I am updating my query in question with the working query for others.
– Qarar Ul Hassan
Nov 25 '18 at 8:38
add a comment |
1
Thanks for your answer @dingo_d. Got it a bit late but it is working now. I am updating my query in question with the working query for others.
– Qarar Ul Hassan
Nov 25 '18 at 8:38
1
1
Thanks for your answer @dingo_d. Got it a bit late but it is working now. I am updating my query in question with the working query for others.
– Qarar Ul Hassan
Nov 25 '18 at 8:38
Thanks for your answer @dingo_d. Got it a bit late but it is working now. I am updating my query in question with the working query for others.
– Qarar Ul Hassan
Nov 25 '18 at 8:38
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456766%2fnot-able-to-fetch-posts-using-cpt-categories%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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