Randomly load categories with latest post

Never ever use query_posts, it creates an endless amount of issues as it breaks the main query object on which so many build in functions, custom functions and plugin rely on. It is pure evil and you should avoid it as such as well. For custom queries, use WP_Query or get_posts, if you simply need … Read more

How to get posts from a current post’s month?

There is date_query which can handle this and an SQL query is not a good practice when you can take advantage of WordPress Query API: $pid = 1; // post ID here $args = array( ‘posts_per_page’ => -1, ‘post_type’ => ‘post’, ‘date_query’ => array( array( ‘year’ => get_the_date(‘Y’, $pid), ‘month’ => get_the_date(‘m’, $pid) ), ), … Read more

Display Posts on Custom Page

If News is a category of posts, then: First, find out the category ID, login to wp-admin, go Posts->Categories, mouse over the ‘News’ category, and cat id should be shown under the left corner of the browser (for chrome this works, not sure about other browsers). Add the following code into your function.php function my_news_category( … Read more

Get $post Object from another page

This is how you can get the post object of a page with a matching path, minus the /en part: $page_path = get_page_uri( get_queried_object_id() ); // en/page/child-page $target_page_path = str_replace( ‘en/’, ”, $page_path ); // page/child-page $target_page = get_page_by_path( $target_page_path ); // WP_Post of target page.

Each post is showing twice in my custom query…?

There is really no reason to use raw sql to query posts. You can accomplish just about any type of query using the WordPress API. For full reference see the codex, Function Reference query posts. Try the following: global $post; $args=array( ‘post_type’ => ‘tutorial’, ‘orderby’ => ‘comment_count’, ‘posts_per_page’ => 10 ); $posts= query_posts($args); foreach ($posts … Read more

Why this code causes infinite loop?

It was correctly pointed out what the issue is, but I also want to explain it so you have better idea of mechanics: when you use query_posts() it writes down generated WP_Query object into global $wp_query variable; have_posts() and the_post() functions are wrappers for methods of same name called using global $wp_query object; so what … Read more

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