Show Child post of Specific Parent on any page in Wordpress - wordpress

I want to be able to use a shortcode to list a child post for a specfic parent on any page (custom post types mainly).
I found this: Show Child-pages of Specific Parent on any page in Wordpress
and it's perfect, but only for "pages" I think, it is not working for me with Custom Post Types.

Guillem,
You can keep use the code from this link.
Just add your custom post types in wp_list_pages function.
So, you will have:
$childpages = wp_list_pages( array(
'child_of' => $post->ID,
'title_li' => '',
'post_type' => 'YOUR_CPT_SLUG',
'echo' => 0,
) );

Related

Wordpress Custom Post Type category used as homepage

I have a Custom Post Type called portfolio and a custom taxonomy portfolio_category that has a term called narrative.
I'm able to access the archive page using the URL /portfolio-category/narrative/.
I want the homepage to display all the items the same as on the narrative archive page without using a redirect.
I've added the following to functions.php
function custom_front_page($wp_query){
if($wp_query->get('page_id')==get_option('page_on_front')){
$wp_query->set('post_type','portfolio');
$wp_query->set('page_id',''); // empty
// fix conditional functions
$wp_query->is_page = false;
$wp_query->is_archive = true;
$wp_query->is_post_type_archive = true;
}
}
add_action('pre_get_posts','custom_front_page');
This is displaying all of the portfolio items on my homepage, but I would like it to be just showing the narrative items.
I've tried this in the php template file, but it isn't working either
<?php
$custom_query_args = array(
'post_type' => 'portfolio',
'portfolio_category' => 'narrative',
);
$custom_query = new WP_Query( $custom_query_args );
?>
How can I get just the narrative items for to show on the homepage?
You're on the right lines but you are not searching by custom taxonomy correctly. Searching by custom taxonomy is different than searching by categories.
If you take a look at the WP_Query documentation for searching by taxomony, you will see that you need to use tax_query to search posts for a custom taxonomy term.
You can use the code below in the template file you use for your homepage. The comments explain what each part does:
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 5, // number of results to get, or -1 to get all of them
'tax_query' => array(
array( // important note, this is an array inside the tax_query arrays
'taxonomy' => 'portfolio_category', // your custom taxonomy
'field' => 'slug', // what to search, e.g. slug, term_id, name
'terms' => 'portfolio' // the term(s) to search for. If you want to search for multiple terms, you can use an array
)
)
)
// do a new query with your arguments
$portfolio_query = new WP_Query($args);
// if the query returns any results, loop through them and process them as required
if( $portfolio_query->have_posts() ):
while ( $portfolio_query->have_posts() ) :
$portfolio_query->the_post();
// do stuff with the post here, e.g. display the post...
endwhile;
endif;
// IMPORTANT! The custom query will overwrite the $post values that are used in the the_post etc.
// This restores the the current post in the main query - i.e. your homepage post.
wp_reset_postdata();

WPBakery Post Grid Custom Query

I am using WPBakery and I would like to use the Post Grid to display the child pages of the current page. I understand that I can use a custom query within WPBakery, however, I am struggling with fetching the current post ID which is accepted by WPB.
I have a custom post type called 'partners' and some have child pages which I would like to bring through using the grid.
I'm looking to turn this into a custom query which is accepted by WPBakery.
$args = array(
'post_parent' => $post->ID,
'posts_per_page' => -1,
'post_type' => 'partners',
);
When use the Post Grid to display the child pages of the current page.
I use string:post_parent="parent page ID"&posts_per_page=-1&post_type=page

How to get the categories of custom post type?

I have a custom post type. In that post type the taxonomy is category and I have to display the all the categories of the custom post type i.e. project.
I want to like option list.
My custom post type is "Project".
In actual custom post types are same as normal posts in WordPress. The only difference is there isn't any in-built WordPress functions to get the information you need, but you can get it by using the wp_query class. This offers a nice way to access all the information in your WordPress database.
To get the custom post type categories you need to change the arguments passed into the wp_list_categories function.
You need to define the taxonomy argument.
If you have a custom post type for your products then to display all the categories for products you need to use the following snippet.
$customPostTaxonomies = get_object_taxonomies('products');
if(count($customPostTaxonomies) > 0)
{
foreach($customPostTaxonomies as $tax)
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'title_li' => ''
);
wp_list_categories( $args );
}
}

Wordpress - adding custom post type category to menu

I have a custom post type named 'The Books', and a relative category named 'The Books' for these custom posts.
When I add the category The Posts to my nav menu, it doesn't work because it goes to the URL /category/the-books instead of just going to /the-books. If I posted this in the default post section it shows correctly, but when I post in the custom post section it does not return my post. I can, of course, add individual posts from my custom post section to the nav menu, but can't figure out how to add an archive page of the custom posts.
My permalinks are set to: URL/%postname%/ so I'm not sure why that is happening.
Here's the function for my custom posts:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'hpl_books',
array(
'labels' => array(
'name' => __( 'The Books' ),
'singular_name' => __( 'Book' )
),
'taxonomies' => array('category'),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'the-books'),
)
);
}
Any advice is greatly appreciated.
thanks!
You shouldn't need to add a category "the-books" in order to display the results.
Have you created a view in your page-templates directory called "archive-hpl_books.php?" That's the file WordPress will look for to display the archive of your custom post type. Basically, you would create a page called "the-books" or whatever, then set archive-hpl_books.php as the template.
See http://codex.wordpress.org/Template_Hierarchy
I'm only responding to this because I just went through a similar issue, so I'm down to help out. :)

How do I display just the children of the parent term on a taxonomy archive page?

Basically, I have created a custom post type and a custom taxonomy for that custom post. The custom taxonomy is hierarchical and the client plans on adding hundreds of categories. Because of this, they want the main page to display only the top level parent which is easy enough. However they want a drill down menu which only shows the parent and the children of the parent so they don't overwhelm the user. I think that what I basically need to do is get the parent id of the child so I can call up just those children.
I have been digging around and the code below is what I have come up with which I know if still far off from what it needs to be. Can you shed any light on this or at least give me a push in the right direction? I'm totally lossed.
I included the code below but I also put in pastebin at http://pastebin.com/B8qtz6Lf
<?php if (is_tax()) {
$this_term = get_term();
if (get_term_children($this_term->term_ID) != "") {
echo "<h2>Subcategories</h2>";
wp_list_categories( array (
'title_li' => '',
'depth' => '1',
'child_of' => '.$this_term->term_ID'
));
}
} else {
wp_list_categories( array(
'taxonomy' => 'compliance_categories',
'title_li' => '',
'depth' => '1'
));
}
?>
Any help you can give me would be awesome!
Not sure if this may help, but here is how I've been able to query a custom taxonomy within a custom post type. This may help you achieve what you're looking for.
$loop = new WP_Query(array('post_type' => 'products', 'product-type' => 'projectors'));
while ($loop->have_posts()) : $loop->the_post();
In this query, the custom post type is "products", the taxonomy is "product-type" and the category within that taxonomy is "projectors". This query will only show anything in the "projectors" category.
Hope that can give you some ideas to try.

Resources