How can i share WP_Query between multiple functions?

If you want to reduce code duplication, then extract the code into its own function, e.g.: function get_xyz_query_args( ) : array { return array(‘post_type’ => ‘accomodation’, ‘posts_per_page’ => 4, ‘meta_query’ => array(‘key’ => ‘accomodation_type’, ‘value’ => get_query_var(‘accomodation_type’))); } …. $args = get_xyz_query_args(); $q = new WP_Query( $args ); However, there is very little performance to … Read more

wp_query to print posts if have X custom field value

You could use meta_query, it can handle arrays and strings. So something like this // get location meta, will be easier to determine what is the type of value $location = get_post_meta(get_the_ID(), ‘Location’,true); $posts = get_posts([ ‘posts_per_page’ => 10, ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘orderby’ => ‘date’, ‘meta_query’ => [ [ ‘key’ => ‘Location’, … Read more

Adding a tax_query to a WP_Query Object

If you instantiate WP_Query with arguments, as in your second example, then the query is performed right away, and setting new arguments will not change the results. $q = new WP_Query( [ ‘post_type’ => ‘post’ ] ); $q->set( ‘tax_query’, [] ); // Too late. If you instantiate WP_Query without arguments, like this: $q = new … Read more

Ajax future single post query doesn’t work when NOT logged in

usually it because you are missing an “add_action” call for none logged in users. add this to your plugin or functions.php add_action(‘wp_ajax_nopriv_REPLACETHIS’, ‘ajax_get_date_post’); and change the REPLACETHIS to your action value from the ajax call. remember that when calling ajax from front-end and you want to allow none logged in users to make these calls … Read more

WP_query exclude posts of a format

tax_query takes an array of arrays. Try this: $myposts = new WP_Query(array( ‘tax_query’ => array( array( ‘taxonomy’ => ‘post_format’, ‘field’ => ‘slug’, ‘terms’ => array(‘post-format-quote’), ‘operator’ => ‘NOT IN’ ) ), ‘posts_per_page’ => 3 ) ); Further reading: http://ottopress.com/2010/wordpress-3-1-advanced-taxonomy-queries/