Replacing search results with custom external query

You can’t return a query object from a pre_get_posts action. In fact, it shouldn’t return anything, the query object is passed by reference. Your second attempt is close, you can unset the s query var so WordPress doesn’t try to search on that term. Of course, now s has no value, so any functions that … Read more

pre_get_posts – Trying to get property of non-object warning

$screen may not be available yet when pre_get_posts fires. Try this instead: public function my_post_type_default_sort( $query ){ if(!isset($_GET[‘orderby’]) && is_admin()) { $orderby = ‘0’; } if($query->is_main_query() && is_admin() && is_post_type_archive(‘my_post_type’) && $orderby=0) { $query->set(‘orderby’,’scr_date’); $query->set(‘order’, ‘ASC’); } }

how would i change post->ID to work correctly when querying pages?

Try with get_the_ID() $image_uploaded_meta_id = get_post_meta(get_the_ID(), ‘_listing_image_id’, true); get_the_ID() is a core WordPress function that acts directly inside the WordPress Loop, retrieving the id of the current item being loaded. $post->id is not working because the variable $post has no scope inside the loop. Here’s the link to the documentation: https://developer.wordpress.org/reference/functions/get_the_id/

Include posts from some categories while excluding from others

You could use a Taxonomy Query, like so: $args = array( ‘tax_query’ => array( ‘relation’ => ‘AND’, array( ‘taxonomy’ => ‘category’, ‘terms’ => array(), ‘operator’ => ‘IN’ ), array( ‘taxonomy’ => ‘category’, ‘terms’ => array(), ‘operator’ => ‘NOT IN’, ), ), ); $query = new WP_Query( $args ); This will query posts that are in … Read more

Ordering posts by an array

This is not possible with native API and you will probably have a rather bad time hacking up SQL for it. This would be quite trivial to re–sort posts like this after they are retrieved with PHP code. if you use get_posts() you get array you manipulate already and for a WP_Query object array of … Read more

Can’t sort custom post type by random

You could use meta_query to search in the meta data, and then sort them as random: $args = array( ‘posts_per_page’ => 4, ‘post_type’ => ‘projekte’, ‘orderby’=> ‘rand’, ‘post__not_in’ => array( get_the_ID() ), ‘meta_query’ => array( array( ‘key’ => ‘ecpt_skills’, ‘value’ => $projektart, ‘compare’ => ‘LIKE’, ) ), ); There is also a mistype in your … Read more

Can I modify a WP_Query instance or clone it with modifications?

WP_Query can repeatedly loop through the retrieved posts multiple times, but it was never meant to query them repeatedly/iteratively. You can just do $query->query($args) again, but there is little point as opposed to just making a new clean object. In your case I think it would make sens to have your custom function accept optional … Read more

Meta Query doesn’t works as espected

Mariolex, Have you tried using meta_query? I wrote something quick and untested but it may do the trick: $args = array( ‘post_type’ => ‘posts’, ‘showposts’ => ‘-1’, ‘meta_query’ => array( array( ‘key’ => ‘pickup_address’, //meta_key ‘value’ => ’43 Longview Drive, Papamoa’, //meta_value ‘compare’ => ‘LIKE’, //compare ), ), ); $query = new WP_Query( $args ); … Read more

Customy WP Query Args are convertet in SQL result

As the dev note for 4.8.3 implies, you need to unescape the escaped % by using the $wpdb->remove_placeholder_escape method. Something like global $wpdb; add_filter(‘posts_request’,array($wpdb, ‘remove_placeholder_escape’)); Very untested.