Delete Associated Media Upon Page Deletion

How about this? It adapts an example on the get_posts() function reference page. function delete_post_media( $post_id ) { $attachments = get_posts( array( ‘post_type’ => ‘attachment’, ‘posts_per_page’ => -1, ‘post_status’ => ‘any’, ‘post_parent’ => $post_id ) ); foreach ( $attachments as $attachment ) { if ( false === wp_delete_attachment( $attachment->ID ) ) { // Log failure … Read more

Difference between an archive and a page listing posts

Archive Page An archive page is part of the WordPress Template Hierarchy, and is the template file WordPress uses to display the archive index list for a given post type. The custom post type archive template hierarchy is as follows: archive-{posttype}.php archive.php index.php WordPress uses the query parameters to output this page, and posts are … Read more

simply loop through posts

Because you’re on a page, that’s only going to display the query for that page. As such, you’d have to create a new query to bring in the posts you want. Replace your loop with this: <?php $args = array( ‘post_type’ => ‘post’ ); $post_query = new WP_Query($args); if($post_query->have_posts() ) { while($post_query->have_posts() ) { $post_query->the_post(); … Read more

Changing the post date and time with function

Call wp_update_post() with a special value for ‘post_date’ and ‘post_date_gmt’: $time = current_time(‘mysql’); wp_update_post( array ( ‘ID’ => 123, // ID of the post to update ‘post_date’ => $time, ‘post_date_gmt’ => get_gmt_from_date( $time ) ) );

How to show related posts by category

The question has already been asked and the answer has been posted too, How to display related posts from same category? Add this code inside your single.php after a loop wherever you want to show related post, <?php $related = get_posts( array( ‘category__in’ => wp_get_post_categories($post->ID), ‘numberposts’ => 5, ‘post__not_in’ => array($post->ID) ) ); if( $related … Read more

Get current menu_order

If you have the post with an $id: $thispost = get_post($id); $menu_order = $thispost->menu_order; WordPress itself does not provide a function to get the menu_order, so you have to query the post-Object. If you are outside the loop, you can use the above function, however inside the loop you could also achieve this by: global … Read more

How to make comments work for a post loaded per Ajax?

To quote the Codex on the have_comments function: This function relies upon the global $wp_query object to be set – this is usually the case from within The Loop The problem is that your ajax handler creates its own WP_Query object. Note that you are not calling the_post(), instead you are calling $posti->the_post(). Same logic … Read more