How does filter the_posts work?

First thing, in your code you are using the_post hook but in your question you are asking about the_posts hook, which are two different things. the_posts gets called just after the posts have been selected from the database and it passes an array of $posts to your function, so you should use that. as for … Read more

Find the post an attachment is attached to

So, if you start with this: $all_images = get_posts( array( ‘post_type’ => ‘attachment’, ‘numberposts’ => -1, ) ); Then $all_images is an array of objects. Step through each one: foreach ( $all_images as $image ) {} Inside that foreach, you can use the normal parameters available to the $post object: $image->ID is the ID of … Read more

How to merge two or more WordPress posts?

The requirements you are describing require much more of an explanation than is really suitable here. A simple solution might be to mark the duplicate posts as duplicates via a meta box on the post screen. This meta box would have one input: A reference to another post. Check out this tutorial on how to … Read more

How to automatically generate a unique random slug

Use the function wp_unique_post_slug(). Do not reinvent the wheel, this one is quite tricky. Usage: $unique_slug = wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ); Then you can test if $slug === $unique_slug and generate a new one if the test fails. You can find the function in wp-includes/post.php. It ends with a filter ‘wp_unique_post_slug’, so … Read more

Can I force a metabox to be in one column?

If you want the meta box to appear below the content area, use the context parameter ‘normal’. See Function Reference/add meta box – Parameters. Initially, your meta box should only go to the side bar if the context parameter is set to ‘side’. As far as making it impossible for the user to drag it … Read more

Strategy for handling hierarchical pages with empty parent content

I am using two strategies here… 1) is simple redirection to first child (using menu order) page-redirect.php <?php /* * Template Name: Redirector * Description: Empty Holder (redirect page) */ $rp = new WP_Query(array( ‘post_parent’ => get_the_id(), ‘post_type’ => ‘page’, ‘order’ => ‘asc’, ‘orderby’ => ‘menu_order’ )); if ($rp->have_posts()) while ( $rp->have_posts() ) { $rp->the_post(); … Read more

How to schedule multiple posts while adding new

Here’s a sketch of another idea, where we create scheduling shortcuts to make it easier for the users: We can use the post_submitbox_misc_actions to add extra fields to the submit box: /** * Scheduling-Shortcuts fields. * * @see http://wordpress.stackexchange.com/a/168748/26350 */ add_action( ‘post_submitbox_misc_actions’, function() { if( ! current_user_can( ‘publish_posts’ ) ) return; ?> <div style=”padding: 2px … Read more

Display all posts from specific categories on a page

I’d Advise adding the arg of the category in an array. And don’t use query_posts. Also showposts is deprecated use posts_per_page instead. $args = array ( ‘cat’ => array(2,6,9,13), ‘posts_per_page’ => -1, //showposts is deprecated ‘orderby’ => ‘date’ //You can specify more filters to get the data ); $cat_posts = new WP_query($args); if ($cat_posts->have_posts()) : … Read more

disable WP automatically inserted line breaks after an image

If this is how it looks: <img class=”alignleft size-medium wp-image-8530″ alt=”…” src=”#” width=”413″ height=”275″ /> <img class=”alignleft size-medium wp-image-8529″ alt=”…” src=”#” width=”413″ height=”275″ /> Then you have to put them together like this: <img class=”alignleft size-medium wp-image-8530″ alt=”…” src=”#” width=”413″ height=”275″ /><img class=”alignleft size-medium wp-image-8529″ alt=”…” src=”#” width=”413″ height=”275″ /> Because if you do this … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)