WP_Query arguments to fetch custom post type posts which are in certain category?

If you want your main query to take in account your custom post type, you can do as shown below : function add_custom_post_type_to_query( $query ) { if ( $query->is_archive() && $query->is_main_query() ) { $query->set( ‘post_type’, array(‘post’, ‘my_custom_post_type’) ); } } add_action( ‘pre_get_posts’, ‘add_custom_post_type_to_query’ ); You can place the above code in your theme’s functions.php or … Read more

Lazy Load using WP_Query pagination

Take a look at this rough example. This requires a little bit of adaptation but as a whole, it does what you want – it loads X amount of next posts if user clicks a button which should be below already loaded posts. If you want to automatically load more posts if user scrolls down, … Read more

I want to query posts in monthly basis

First, get the current month and year: $month = int(current_time(‘m’)); $year = int(current_time(‘Y’)); Next, get the posts in this month: $query = new WP_Query( ‘year=” . $year . “&monthnum=’ . $month ); If the query returns empty query the month before and repeat this until you have found a non empty month: while (empty($query)) { … Read more

Exclude posts from featuring

Excluding specific categories from WP_Query This is in the codex. You can exlclude specific categories from WP_Query. Where array(2, 6) are IDs for categories to be excluded in this example. $query = new WP_Query( array( ‘category__not_in’ => array( 2, 6 ) ) ); See the codex: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters NOTE: If you are trying to exclude category … Read more

Modifying the Search Results

add_filter( ‘pre_get_posts’, ‘__filter_pre_get_posts’ ); function __filter_pre_get_posts( $query ) { // this is to detect a public search query if ( ! is_admin() && $query->is_search ) { // DO your magic here… } }

medoo framework in WP plugin

medoo and other similar libraries are generic, that is they are aimed at querying some database in general and the weight of figuring out necessary queries are on you as developer. WP_Query and other WP APIs that perform requests to database are much more specific. They already contain much of that logic, which allows you … Read more

Get posts for last working week in WP_Query

Well, try the below code- $base_array = array( ‘posts_per_page’ => -1, ‘fields’ => ‘ids’, ‘post_type’ => ‘cpt’, ‘post_status’ => array(‘publish’), ‘date_query’ => array( ‘after’ => strtotime( ‘previous week Monday’ ), ‘before’ => strtotime( ‘previous week Friday’ ) ) ); $base = get_posts($base_array); I’ve not tested it. But I tested that below code returns the perfect … Read more