Add new post in existing categories using wp_insert_post

post_category is only for Core Categories. Use tax_input instead: $new_post = array( ‘post_title’ => esc_attr(strip_tags($title)), ‘post_content’ => esc_attr(strip_tags($description)), ‘post_type’ => ‘proiecte’, ‘post_status’ => ‘publish’, ‘tax_input’ => array( ‘categorie’ => array(17,16) ) ); $post_id = wp_insert_post($new_post); It may be clearer to other developers, and yourself in the future, if you use something other than categorie – … Read more

Automatically set post_parent value

You can do that easily using save_post hook like so: function set_post_parent_on_save($post_id) { // If this is just a revision, don’t do anything if ( wp_is_post_revision( $post_id ) ) return; // Check if it is a post if ( ‘post’ == get_post_type( $post_id ) ) { // unhook this function so it doesn’t loop infinitely … Read more

How to create a page that shows the last 10 posts?

By default WordPress displays your content in a blog format on the homepage. Using Default Static and Blog Page Settings in WordPress. WordPress comes with built-in support for creating a custom home page (static front page), and a separate page for blog posts. To use this method, you need to create two new WordPress pages. … Read more

Where Should i write the code for wordpress ajax voting?

Use REST API endpoint instead! Lets register an endpoint at /wp-json/tomjn/v1/test, that calls tomjn_rest_test() when you hit it: add_action( ‘rest_api_init’, function () { register_rest_route( ‘tomjn/v1’, ‘/test/’, array( ‘methods’ => ‘GET’, ‘callback’ => ‘tomjn_rest_test’ ) ); } ); Now lets add the tomjn_rest_test function: function tomjn_rest_test( $request ) { return “moomins”; } Now when we visit … Read more

Output Post with ACF Fields into other Post

To not duplicate code you could make a file in your theme/child-theme for the html of the post which you intend to embed, let’s say x_cpt_render_html.php: function get_x_cpt_html(){ ?> <div class=”x-cpt”> <h2> <?php the_title(); ?> </h2> <div class=”content”> <?php the_content(); ?></div> <div class=”custom”> <?php the_field(‘custom’); ?> </div> </div><?php } Then you could embed this file … Read more