Custom rewrite rule for hierarchical custom post type

You’re pretty close. Your rewrite rule is using the wrong query var, pagename should be just name. Here’s a version that works for me on a fresh 4.4.1 install and twentysixteen theme- function bvt_product_init() { $args = array( ‘label’ => __( ‘Product’, ‘domain’ ), ‘description’ => __( ‘Company products’, ‘domain’ ), ‘supports’ => array( ‘title’, … Read more

Push metadata in array

You need to loop through your posts, and retrieve the metadata for each post. Add them to your array, and encode them to JSON. $posts = $query->get_posts(); foreach( $posts as &$thispost ) { // reference the current item $thispost->meta = get_post_meta( $thispost->ID ); } Now you got a WP_Post-Object, with all the post meta available … Read more

How to display all posts from standard posts to custom post types in a loop with pagination?

You don’t need a custom query to include CPTs in homepage posts in normal cases. Just use pre_get_posts. function wpse215208_include_all_cpt( $query ) { if ( is_home() && $query->is_main_query() ) { $query->set(‘post_type’, array( ‘post’, ‘photo’, ‘video’, ‘web’ ) ); } } add_action(‘pre_get_posts’, ‘wpse215208_include_all_cpt’);

How to get updated data when save_post triggers?

I’m going to post this as an answer, because it worked for me: function my_awesome_func ($post_id, $post, $update) { //update the value we need early update_post_meta($post_id, ‘my_meta_key’, $_REQUEST[‘my_meta_key’]); $newValue = get_post_meta($post_id, ‘my_meta_key’); } add_action( ‘save_post_my-custom-post-type’, ‘my_awesome_func’, 10, 3 ); You could also simply use the value of $_REQUEST[‘my_meta_key’] directly if that works, but for my … Read more

single-{cpt}.php ignored

Any template file must be in the current active theme folder. If they are in a different location, they won’t be automatically loaded, instead you need to code and modify the template file location determined by WordPress template system: add_filter( ‘template_include’, ‘envento_type_templates’, 99 ); function portfolio_page_template( $template ) { if ( is_archive( ‘evento_type’ ) ) … Read more

How to check post type (to include custom css)

Yes too early is probably why, the global $post variable is not populated yet. Try hooking to a later action (I think init at the very least): add_action(‘wp_enqueue_scripts’,’enqueue_product_styles’); function enqueue_product_styles() { if (get_post_type() == ‘product’){ wp_enqueue_style( ‘css_products.css’, get_stylesheet_directory_uri().’/assets/css/css_products.css’, false ); } }