Querying post from a multisite network

You could use your list of blog ids in this way … $posts = array(); foreach ( $your_list_of_blog_ids as $blog_id ) { switch_to_blog( $blog_id ); $query = new WP_Query( array( ‘post_type’ => ‘any’, ‘posts_per_page’ => 10, ) ); while ( $query->have_posts() ) { $query->next_post(); $posts[] = $query->post; } restore_current_blog(); } Important are switch_to_blog and restore_current_blog. … Read more

Accessing the post content with WP_Query

First of all, don’t use and abuse the $wp_query global variable. This global should be reserved to the main query only. Use any other variable that will not create conflict. Secondly, don’t use the raw WP_Post properties. These are raw and unfiltered. WP_Query does set up postdata by default which make the use of the … Read more

How do I reorder (pop and push) items from WP_Query?

You could use the Metadata API to retrieve the rw_advertising_position metadata for each post, seperate the ads from the content, and then re-insert the ads at the proper locations: /** * Extracts from an array posts with positional metadata and re-inserts them at the proper * indices. See https://wordpress.stackexchange.com/questions/210493 **/ function wpse_210493_apply_advertising_position( &$posts, $return = … Read more

Restore contents of deleted user

None that don’t involve a backup. If you have a backup, you’ll have to do some work to restore only a certain user’s data without resetting everything to that point. I’d say import the backup into an empty database, then copy data over either with SQL (INSERT INTO wp_posts SELECT FROM otherdb.wp_posts WHERE post_author = … Read more

Pretty URL with add_query_var

add_rewrite_endpoint( ‘changelog’, EP_ROOT ); Will add the endpoint, changelog, which you can then check on template_redirect hook. On match the query variables ( $wp_query->query ) array should contain key changelog containing whatever comes after the / in value. So for URL example.com/product1/changelog/5 you would have a query variable named changelog with the value 5. If … Read more

Verify if tag is used on posts

Try using the has_tag() conditional template tag. e.g., to query for the tag “foobar”: <?php if ( has_tag( ‘foobar’ ) ) { // The current post has the tag “foobar”; // do something } else { // The current post DOES NOT have the tag “foobar”; // do something else } ?> If you’re inside … Read more

delete post also attachments

Maybe this works function remove_post() { if(isset($_POST[‘post_id’]) && is_numeric($_POST[‘post_id’])) { $post = get_post($_POST[‘post_id’]); if(get_current_user_id() == $post->post_author) { $args = array( ‘post_parent’ => $_POST[‘post_id’] ); $post_attachments = get_children($args); if($post_attachments) { foreach ($post_attachments as $attachment) { wp_delete_attachment($attachment->ID, true); } } wp_delete_post($_POST[‘post_id’], true); } } exit; } The code added function get_attachment_id_from_src ($image_src) { global $wpdb; $query = … Read more

Get title of post without using the_title();

This is because the_title() echos the post title (see the linked documentation). Use get_the_title() instead which returns the title as a string. Edit You have two options: Use get_the_title() to return, rather than echo, the post title Filter the_title to echo a custom string as the post title Using get_the_title() <?php // NOTE: Inside the … Read more

Different permalink for posts and authors

I would just change the author_base on the global $wp_rewrite object. Also add a field to the Permalink options page, so you can change it at will. To start: a class to wrap everything up. <?php class Custom_Author_Base { const SETTING = ‘author_base’; private static $ins = null; public static function instance() { is_null(self::$ins) && … Read more

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