Why are you using $wpdb
to query from database. Use WP_Query
or get_posts
instead. This is how you can query from WordPress database and get 4 latest posts from category id 24.
<?php
$args = array(
'post_type' => 'post',
'cat' => 24,
'posts_per_page' => 4,
'ignore_sticky_posts' => 1
);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
?>
Related Posts:
- How to display SQL query that ran in query?
- How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
- wpdb get posts by taxonomy SQL
- Multipart/formatted MySQL query problem
- WordPress creating excessive joins on meta_query with search
- cron job to auto delete posts of a specific post type older than x days
- Are database queries created using WordPress filters protected from SQL injection?
- Get posts by category with pure SQL query
- Custom query to get terms from post ids
- Get the timout value of a saved transient?
- What’s wrong with my $wpdb prepare?
- How do I see the mysql query generated by get_posts( $args )?
- sql select query in wordpress ‘page’ [closed]
- problem with sql query in wordpress plugin
- Attempt to improve WP search, can someone check my SQL query?
- wpdb query problem to access previous 3 days posts
- I can’t figure out what’s wrong with this statement. $wpdb->query update
- Custom Query – Based on user input
- how to insert missing tags into the posts through mySQL?
- $wpdb->prepare affecting the query?
- WordPress SQL search, how to handle SQL Injection?
- exclude pingbacks from wordpress SQL query
- prepare function sql safe method
- WordPress SQL query to tag all posts containing a specific word on title
- Select column name dynamically mySQL Query
- How do I query for posts by custom meta and those that have been stickied?
- Change pure SQL database query to WordPress post query?
- Cannot get sql request from Query object?
- I want to get on those users their meta value are like “AGENT” .. but this query is not working
- Display data from phpMyAdmin with WordPress
- SQL to Query the db and return all posts and it’s metas
- SQL query to delete users with multiple meta keys and comments
- Nested select statements not working
- Speed up search query that searches in post meta?
- Are there any best practices for creating a Like/Favourite feature in WordPress using custom MySQL tables and without any plugins?
- Fastest and most efficient SQL query to check if UID exists
- How to make MySQL search queries with quotes
- How i make a custom sql query for Woocommerce
- $wpdb query for price in custom field value
- Run an update query in a function
- Slow query when joining wp_posts with a lookup-table?
- Wpdb generates too many queries
- learn to run wpdb class
- SQL Query : how copy all tags of post into their post content in wordpress by sql query
- Where can I find the SQL to get the most used information by wordpress database?
- Custom Query for wp_posts using wp_postmeta
- 3 queries to update WordPress
- How can I create a meta_query with an array as meta_field?
- Query multiple meta key values?
- Is there a way to list all used/unused WP templates?
- Is there a way of increasing the speed of this query?
- How can I query all users who registered today?
- Reversing the order of posts AFTER the query is performed
- what are the numbers between curly brackets in search query
- When/why does ‘$query->get( ‘tax_query’ );’ return empty?
- How to get link and title of next and previous post on single page
- Search custom post type by meta data
- Remove [gallery] shortcode altogether
- How to make an activities stream mixing posts and comments?
- query multiple taxonomy and show post count
- SQL query equivalent to WP User Query
- Categories and products in random order
- add_query_arg() XSS Vulnerability
- Change searched term
- Query Posts By Post Publish Date, but sort by Custom Meta Key
- List of the years with posts presented
- MySQL variable in query
- How to execute a SQL-query which contains multiple queries using $wpdb->query?
- Only show upcoming event or current events
- Why a URL with a query is always slower than that without a query?
- Maintaining Queries in URL
- Editing the default wordpress search
- How to orderby multiple meta fields with another meta query
- Query posts using meta_key
- MySql Query very slow
- How to change the amount of posts previewed on a page?
- How to get events using multiple custom meta fields?
- Overriding default calendar to show posts from a category
- Random Page Rewrite
- WordPress Mysql query and Duplicate
- How to “orderby” the first array in a meta_query that uses multiply keys?
- Order posts ascending from posts in array
- custom sorting media with multiple filters – error: not unique table/alias: wp_postmeta
- How can I get posts by 2 meta keys, prioritising one of them?
- SQL query to list all posts of specific post type with a specific set of associated postmeta values as a column
- implementing my wp_query using $wpdb class
- Category_in return empty
- How to run select query of post with category and tags?
- modifying main query to query for ranges e.g. from 1999 to 2016
- Querying users by meta value and getting a strange answer
- Problem on wpdb queries, wp_insert_post, wp_insert_user etc. The query runs twice
- Sort order in get_posts [closed]
- Query unique author ids with published post of type job
- Custom Query for searching through custom fields
- Query posts alphabetically within this function
- Conditional Tag Custom Querys?
- query posts only works on the first page
- WP Query outputs three items within a div
- How to show sticky posts on all pages of the pagination, not just the first page?
- Batch Scanning and Deleting Empty WordPress Posts