How to query for posts (hierarchical custom post type) that have children but are NOT top level?

You can use filters to modify the SQL. I added something unique in the post_type so I could filter only this query and not change anything else. The query for example $query = new WP_Query(array(‘post_type’=>’something_unique_for_filter’)); And the filter function custom_where($where, $query) { global $wpdb; if(false !== strpos($where, ‘something_unique_for_filter’)) { $where = ” AND post_type=”page” AND … Read more

extract serialized array to use for wp-query

You need to unserialize() if its serialized string. In your case from what I understand you store the array in the user_meta so its serialize it when you store it. but it will unserialize it when you use the get_user_meta so you dont need to unserialize() Your code seems fine. $author_in = get_user_meta($current_user->ID, ‘following_users’, true); … Read more

WordPress meta_query >= &

Looks like I found the solution. I needed to use compare instead of operator in the meta_query so array( ‘key’ => ‘vacancy_small_salary’, ‘value’ => $vacancy_small_salary, ‘operator’ => ‘>=’, ‘type’ => ‘NUMERIC’ ), becomes array( ‘key’ => ‘vacancy_small_salary’, ‘value’ => $vacancy_small_salary, ‘compare’ => ‘>=’, ‘type’ => ‘NUMERIC’ ), and the same for the other query

Find all product ID from array of customfield

$product = array( ‘post_type’ => ‘product’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => -1, ‘meta_query’ => array( array( ‘key’ => ‘product_cip’, ‘value’ => ‘some value’, ‘compare’ => ‘=’, ) ), ‘fields’ => ‘ids’, ); $product_post = get_posts($product); echo count($product_post); if you have get all products data $products_array = array(); foreach ($product_post as $v){ $_product = wc_get_product($v); echo … Read more

How To Keep Search Title the same on paged Results

The problem is here: if( get_post_type()==’page’ || is_single() ){ echo ‘<h1 class=”h2″>’. $post->post_title .'</h1>’; } get_post_type() is using the global $post, which will contain the first post in your result set. If this is a page, then your other conditions will never be tested and you’ll never reach the is_search() test. Use is_page() instead: if( … Read more

How can I insert a new markup element after 4 posts automatically? [closed]

This can be achieved by accessing WP_Query class properties, available while in the loop, like $current_post (index of the current post), $post_count (number of posts displayed), $found_posts (total number of posts matching query). By using $current_post, $post_count and PHP modulo, like shown below: <?php if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); … Read more

WP_Query: apply an SQL function to meta fileld value

You can use the filter posts_orderby. For a simple example: add_filter(‘posts_orderby’, ‘apply_sql_on_order’, 10, 2 ); function apply_sql_on_order($orderby, $query) { if(is_array($query->query_vars[“orderby”]) && array_key_exists(“apply_sql”, $query->query_vars[“orderby”])) { $orderby = preg_replace(“/(.*) (ASC|DESC)/”, $query->query_vars[“orderby”][“apply_sql”] . “($1) $2”, $orderby); } return $orderby; } $query = new WP_Query( array( ‘meta_query’ => array( ‘state_clause’ => array( ‘key’ => ‘state’ ), ), ‘orderby’ => … Read more