How to pass custom parameter to WP_Query for filtering in pre_get_posts

You can access query variables (including custom ones) via the WP_Query::get() method. For example: $my_query = new WP_Query( array( … ‘wpse105219_custom_var’ => ‘foobar’, … ) ); To ‘catch’ this at pre_get_posts: add_action( ‘pre_get_posts’, ‘wpse105219_pre_get_posts’, 10 ); function wpse105219_pre_get_posts( $query ){ if( $query->get( ‘wpse105219_custom_var’ ) == ‘foobar’ ){ //Alter $query } }

WP API to fetch all the media/posts records if count is more than 100

Add a page number query to your request URL, and continue querying next page numbers until you get an empty result: <your_wordpress_install_base_url>/wp-json/wp/v2/media/?per_page=100&page=2 Mark’s suggestion to consider requesting less than 100 per page may be a good one. So for example you may want to do your query this way: <your_wordpress_install_base_url>/wp-json/wp/v2/media/?per_page=25&page=3 Note also that (for me … Read more

WP_Query on different site in a multisite setup

Alternative option could be using the Rest API: Example (post): Search for Jane Doe in posts: https://example.com/staffsite/wp-json/wp/v2/posts/?search=jane+doe Example (custom post types): If the public and searchable custom post type (e.g. staff) has show_in_rest set to true then we can search for Jane Doe in staff posts: https://example.com/staffsite/wp-json/wp/v2/staff/?search=jane+doe ps: Somewhat related, if you want to create … Read more

Order posts by date and then by custom field

add_filter(‘posts_orderby’, ‘posts_orderby’); function posts_orderby($orderby_for_query) { global $wpdb; $prefix = $wpdb->prefix; $orderby_for_query = “LEFT(” . $prefix . “posts.post_date, 10) DESC, ” . $orderby_for_query; return $orderby_for_query; } This produces this query: SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type=”post” AND (wp_posts.post_status=”publish”) AND (wp_postmeta.meta_key = ‘my_post_rating’ ) GROUP BY wp_posts.ID ORDER … Read more

Use post__in and post__not_in together?

post__in and post__not_in are mutually exclusive. Note: you cannot combine post__in and post__not_in in the same query. http://codex.wordpress.org/Class_Reference/WP_Query As your question is written, the solution you reject– that of using array_diff— is the obvious answer. If it is true that that solution won’t work you will need a filter on posts_where (probably) that will provide … Read more

Post queries by latitude and longitude

Close. You need another INNER JOIN and should escape all your variables using $wpdb->prepare. I’ve also included a more efficient Haversine formula (source) to calculate the radius. If you use kilometers, then change the $earth_radius to 6371. Also, a great way to debug is to echo the sql and paste it into phpMyAdmin (or whatever … Read more