get custom post type by tag

You’ll need to setup the post for the query by changing the following line to get rid of the infinite loop. <?php while ($query->have_posts()) : $query->the_post(); ?> If your looking for a custom post type, you’ll need to specify that in the query arguments: <?php $query = new WP_Query( array( “post-type” => “yourposttype”, “tag” => … Read more

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