how does $wpdb differ to WP_Query?

The wpdb class is the interface with the database. WP_Query uses wpdb to query the database. You should use WP_Query when dealing with the native WordPress tables, to integrate your code properly with the WordPress environment. Use wpdb directly when you need to access data in your own tables.

Sort posts by category name and title

To get them broken down by Category, you need to loop through the list of categories and then query on each category: $categories = get_categories( array (‘orderby’ => ‘name’, ‘order’ => ‘asc’ ) ); foreach ($categories as $category) { echo “Category is: $category->name <br/>”; $catPosts = new WP_Query( array ( ‘category_name’ => $category->slug, ‘orderby’ => … Read more

Order by Category and Post in WP custom Query

To do this you have first get all the category in ascending order by get_categories then you have to pass the cat_id in WP_Query to get the post related with that category. $args_cat = [ ‘orderby’ => ‘name’, ‘order’ => ‘ASC’, ‘hide_empty’ => 0, ]; $categories = get_categories($args_cat); //print_r($categories); if (!empty($categories)): foreach ($categories as $category): … Read more

What to do in this case after wp_query has been modified

If the code shown above is the one the plugin is using, it will affect every query, since it’s not checking for is_main_query() (https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts) What you could do, is remove the action, do your query, and then add the action again (if needed) remove_action( ‘pre_get_posts’, ‘category_and_tag_archives’ ); $myQuery = new WP_Query($queryArgs); add_action( ‘pre_get_posts’, ‘category_and_tag_archives’ ); … Read more

Where should you reset postdata?

Short version: As Tom J Nowell said, You shouldn’t cleanup if there’s nothing to clean Long version: However, if you put wp_reset_postdata() after (or outside) the loop, it would still work perfectly fine. I have used the function in various scenarios, including something like this: dynamic_sidebar( ‘main-sidebar’ ); wp_reset_postdata(); The reason was that some widget … Read more

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