How we exclude current post form related posts
Use the parameter ‘post__not_in’ to exclude post IDs: $posts = new WP_Query( array ( ‘post__not_in’ => array(get_the_ID()) // exclude current post ID ) );
Use the parameter ‘post__not_in’ to exclude post IDs: $posts = new WP_Query( array ( ‘post__not_in’ => array(get_the_ID()) // exclude current post ID ) );
get_the_category() only works with the default taxonomy, per the Codex page; you probably need to use get_the_terms() instead. So you’d replace your get_the_category() call with get_the_terms( $post->ID, ‘brands’ );, if I’m not mistaken.
Why don’t you use WordPress get_posts? You can add the parameter orderby => ‘title’ as you want. $args = array( ‘posts_per_page’ => -1, ‘order’ => ‘ASC’, ‘orderby’ => ‘title’ ); $posts = get_posts( $args ); print_r($posts); If you want the sql you can just add ORDER BY post_title: SELECT * FROM $wpdb->posts WHERE post_type=”post” AND … Read more
The very broad answer for why this doesn’t work is that the internet is stateless. Basically each request for a page is a separate and unique instance from other page requests. Example Let’s say post-1 is in category-1 and category-2. When WordPress loads post-1 on single.php it has no way of knowing how the user … Read more
When the foreach loop is from get_posts(), you need to run setup_postdata() for each post if you want to use themplate tags inside the loop like the_category(), if not you can get unexpected behaviour. Also, you will need to run wp_reset_postdata() after the foreach loop. Note that the_category and similar template tags actually displays/echoes the … Read more
You can use the WordPress function wp_get_recent_posts(), with required args. $output = array(); $args = array( ‘numberposts’ => 10, ‘offset’ => 0, ‘category’ => 0, ‘orderby’ => ‘post_date’, ‘order’ => ‘DESC’, ‘include’ => , ‘exclude’ => , ‘meta_key’ => , ‘meta_value’ =>, ‘post_type’ => ‘post’, ‘post_status’ => ‘draft, publish, future, pending, private’, ‘suppress_filters’ => true … Read more
You can adapt your first loop to either show a sticky post or the latest post in the following manner: PLEASE NOTE: You have to reset your custom query <?php $args = array( ‘posts_per_page’ => 1, ‘post__in’ => get_option( ‘sticky_posts’ ), ‘ignore_sticky_posts’ => 1 ); $my_query = new WP_Query( $args ); $do_not_duplicate = array(); while … Read more
the fastest way imho would be to create a custom sql statement: global $wpdb; $rows = $wpdb->query(‘SELECT DATE(post_date) AS date, COUNT(*) AS count FROM ‘ . $wpdb->posts . ‘ GROUP BY DATE(post_date) ORDER BY DATE(post_date)’); foreach ($rows as $row) { echo $row->date . ‘: ‘ . $row->count . ‘<br>’; }
Since you are already getting the ID of the newly created post ($post_id), you just need to get the URL and redirect after the submission. Using get_permalink( $post_id ) you can get post’s URL. Also, you can get rid of the header() function to send a new HTTP header, and use WordPress’ own function for … Read more
Don’t rely entirely on JavaScript validations. Use below hook for Server side validation. function check_if_post_content_set( $maybe_empty, $postarr ) { // Check if post is already created. IMPORTANT if($postarr[‘ID’] && (int)$postarr[‘ID’] > 0){ if( !$postarr[‘post_content’] OR $postarr[‘post_content’] == ” OR $postarr[‘post_content’] == NULL ){ $maybe_empty = true; } } return $maybe_empty; } add_filter( ‘wp_insert_post_empty_content’, ‘check_if_post_content_set’, 999999, … Read more