All posts returned when author of 0 is queried

Welcome to WPSE. The condition you want to prevent is a ID of 0 so only run the query if it does not. Also, DRY – don’t repeat yourself. Rather than call get_current_user_id() multiple times, call it once and store it. $reg_count= 0; $user_id = get_current_user_id(); if ( 0 !== $user_id ) { echo “looking … Read more

Custom WP query with polylang term

Revised Answer If the terms 2, 32, 71 and 38 belong in the same taxonomy like language, then see my original answer. Otherwise, the WP_Query would be like so: $query = new WP_Query( [ ‘post_type’ => ‘property’, ‘tax_query’ => [ ‘relation’ => ‘AND’, // for clauses 1 and 2 [ // clause 1 ‘taxonomy’ => … Read more

how update data through ajax and jquery

First of all, in your jquery event handler the action parameter’s value should be the string after wp_ajax_ and wp_ajax_nopriv_. So in your example the action should be update_records. So display_func is wrong. Then in line 3 of your php code here the echo keyword should be removed. So your php code should be like … Read more

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

unable to use ‘new WP_Query’ in AJAX call

Just to clear this up for someone that might have similar problem, everything was find with query, the problem with this was WordPress was looking for WP_Query class in namespace file, replacing new WP_Query with new \WP_query solved this problem 😀 thanks for help!

How to display all posts divided/ordered by post date? [closed]

You should be able to achieve this with something like the below… <?php $wpb_all_query = new WP_Query( [ ‘post_type’=>’post’, ‘post_status’=>’publish’, ‘posts_per_page’=> -1, ‘orderby’ => ‘date’, ‘order’ => ‘ASC’ ] ); $dividers = []; ?> <?php if ( $wpb_all_query->have_posts() ) : ?> <div id=”list_allposts”> <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?> <?php $post_date = get_the_date( … Read more