New answers tagged

0

The first thing I would recommend is verify the number of posts your query is returning using the following: $apartquery->found_posts


2

the first request don't show any custom post type (polylang_mo is a custom post type) polylang_mo is a private custom post type so unless I explicitly request this post type, it won't be included. the second request shows only 1 page (instead of 3) Polylang was actually filtering the results. Adding --skip-plugins=polylang makes the 3 posts ...


0

So I finally figured it out. Using pre_get_posts and the $_GET global var to check if any query args have been added works a treat. I used the follow code to get the desired results. function my_pre_get_posts($query) { if( $query->is_main_query() && is_archive() && !isset($_GET['orderby']) && // ...


0

So I decided to go with "load more" clicking option but the code I"m using doesn't work, click on load more and nothing happends. Index.php: <div id="content"> <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'multisite' => 1, 'sites__not_in' => array(1), 'paged' => $paged,); $loop = new ...


0

Infinite scroll usually requires some kind of Javascript library to load more results via Ajax. The basic idea is this: User arrives at your page, you show, say, 5 posts per page They scroll past a certain offset and you use JavaScript to trigger an Ajax request Your request passes the requested paged, the endpoint receives the request, and returns just ...


0

If you are altering main query then use pre_get_posts filter instead of WP_Query. add_action('pre_get_posts','wpse224610_alter_query'); function wpse224610_alter_query($query){ $cat_id = get_cat_ID('Featured articles'); $exclude_cat_id = -$cat_id; if( !is_admin() && $query->is_main_query() ){ $query->set( 'cat', ...


0

For those who experienced the same problem, I finally found a way to do the sort. First I created an extra CMB like this : $cmb->add_field( array( 'id' => $prefix . 'session_gooddatebegin', 'type' => 'hidden', ) ); Then I created an action using save_post. If the CPT "session" is being updated, thanks to $update, I assume that ...


0

Wordpress is also provide more filters to change custom queries. https://codex.wordpress.org/Custom_Queries add_filter('posts_join', 'custom_query_join' ); $premium = new WP_Query( $premium_args); remove_filter('posts_join', 'custom_query_join' ); // remove filter bcz not effect to another query to use after this query. function custom_query_join( $join ){ ...


0

Try this function search_pre_get_posts( $query ) { if ( $query->is_search() && $query->is_main_query() ) { $currentdate = date('Y-m-d'); $enddate = strtotime(date('Y-m-d') . "+7 days"); $enddate = date('Y-m-d',$enddate); $query->set( 'meta_query', ...


0

In a nutshell this is a poor format to store date. You get results you get because 05 < 20 < 22 in that example. You are merely comparing strings and in these date strings day of the month is leading. You wouldn't be able to easily cast it to MySQL DATE either since that one got YYYY-MM-DD format. If you need sort you should store your dates in ...


1

If I get it right you are storing this data in user meta. WP_Query would have no remote idea how to retrieve that, since it's only aware of post meta. The two are completely separate and there is no native way to involve one in queries for another. Likely you will have to do this in a two step process: Use get_users() to retrieve IDs for all authors of ...


1

I fixed my problem as follows: // Adds rewrite rules for shop post type function gtp_add_shop_rewrite_rules() { add_rewrite_rule( '^shop/([^/]*)/([^/]*)/([^/]*)$', 'index.php?shop-category=$matches[1]&width=$matches[2]&height=$matches[3]', 'top' ); add_rewrite_rule( '^shop/([^/]*)/([^/]*)$', ...


2

Wordpress also provide more filter to can change wp query using filters. What i need logical to use filter to override query below sample of code if this helpful for you. add_filter( 'posts_where', 'posts_where_condition_search' ); $wp_query = new WP_Query( array( 'post_type' => 'press-release', 'posts_per_page' => ...


0

why not like this? add_action( 'pre_get_posts', 'my_post_queries' ); function my_post_queries( $query ) { if ($query->is_main_query() && !is_admin()) { // maybe check for specific page here // if (is_tax('your-taxonomy') { $query = false; remove_all_actions ( '__after_loop'); // } } } Or did I ...


1

Inside get_posts() of WP_Query class, you will see this code block. /* * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and * 'category_name' vars are set for backward compatibility. */ if ( ! empty( $this->tax_query->queried_terms ) ) { /* * Set 'taxonomy', 'term', and 'term_id' to the * first taxonomy other than 'post_tag' or ...


0

what about WP REST API V2? I think this is not working on v2! add_filter('json_query_var-meta_query', 'adjustQrry', 10, 1); should it be like this? add_filter('rest_query_vars-meta_query', 'adjustQrry', 10, 1);


0

Solved, I removed : 'meta_compare' => '=' !


-1

Scrap this figured it out! A plugin was overriding these settings on the specific archive I need it on! So probably without the plugin, the above would work :)


1

I think you need to do something like this: <?php $args = array( 'post_type' => 'shop', 'post_status' => 'publish', 'posts_per_page' => -1, 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'cu_status', 'value' => array( ...


3

I am assuming your custom post type is also "series". Can you try this, it should work. $args = [ 'post_type' => 'series', 'posts_per_page' => -1, 'meta_query' => [ [ 'key' => 'series', 'value' => 19, 'compare' => '=', 'type'=>'NUMERIC' ] ...


0

Fixed: Search and result the all keys; <?php $the_keys = preg_split('/\s+/', str_replace('-',' ',get_query_var('s')),-1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); $total_keys = count($the_keys); $the_query = new WP_Query(array('post_type'=>'nothing')); if($total_keys>1){ for($i = 0; ...


0

Maybe somebody like me want fuzzy logic results I solved this way: "search.php" <?php $the_keys = preg_split('/\s+/', get_query_var('s'),-1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); $total_keys = count($the_keys); // Count the search term $the_query = new WP_Query(array('s' => get_query_var('s'))); // IF have ...


0

You are getting into deep water quickly. I would look into SearchWP plugin as they provide LIKE searching and Fuzzy matching. SearchWP


0

Not sure of the context for this, is it a custom post type or just a page? This might be something which you're looking for - function add_lead_management_endpoints() { add_rewrite_rule( 'manage-lead/lead_id/([0-9]{1,})/product_id/([0-9]{1,})', 'index.php?pagename=manage-lead&lead_id=$matches[1]&product_id=$matches[2]', ...


1

Fields in database tables can not contain other types of data than text strings and numbers, and variations of them like dates and times. The field meta_value of wp_postmeta is a text field. So, when you store an array as meta_value you are really storing a string containing the serialized data that represents the original array. If you need to interpret ...


0

Based on the codex, the meta_query parameter contains one or more array with the relation parameter not set if single inner meta_query array. Also remove the page parameter as it serves only for a Static Front Page. Your args array should look like that: $archive_args = array( 'post_type' => 'speight_home_plans', 'orderby'=> 'title', ...


0

Use this query: select * from wp_posts where id not in (select distinct post_id from wp_postmeta where meta_key='_your_meta_key')


0

This is more of a logical problem. Lets say you stored location (City/address/Lat-long(Best would be Lat/long) of user in a custom field (There should be a standard way to assign locations to users throughout the site). you want to query posts from users which are near this particular user. Get custom field value of location for this user. Convert this ...


1

You can try this by using meta query to filter results and then adding orderby to order results. Below arguments should work $args = array( 'post_type' => array('post', 'video'), 'meta_key' => 'pp_like_count', 'orderby' => 'meta_value_num', "meta_query" => array( array( "key" => "pp_lang", ...


0

You can use meta query see below link it will help you. How to order by meta field


1

First set a function to be used later with a usort to sort the categories by date of most recent post: function orderByDate( $a, $b ) { return strtotime($a['date']) - strtotime($b['date']); } Get your categories as you were doing: $args = array( 'hide_empty'=> 1 ); $categories = get_categories($args); ...


0

@TJ Sherrill, as alternative of @Milo answer, WordPress say, "If you want to alter the value of an option during the rendering of a page without changing it permanently in the database you can use pre_option_(option name). Here the sample code related to your issue, using is_tax as conditional taxonomy: add_filter( 'pre_option_posts_per_page', ...


1

Use the pre_get_posts action to modify query parameters before the queries are run. You can use many of the Conditional Tags to target specific types of pages. function wpd_alter_posts_per_page( $query ) { // don't alter admin or custom queries if ( is_admin() || ! $query->is_main_query() ) return; // if this is your-custom-tax ...


-1

global $wpdb; $excludeposts = "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND meta_key NOT LIKE %aa_% ORDER BY post_date DESC "; $main_query = new WP_Query( array( 'post__not_in' => $excludeposts, 'paged' => $paged ) ); while ($main_query->have_posts()) : $main_query->the_post(); //Stuff... endwhile; ...


1

You need to set your paged arg in your $args array: $listing_args = array( 'post_type' => 'business', 'posts_per_page' => 10, 'meta_key' => 'listing_num', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'offset' => ($paged -1) * 10, 'paged' => ...


0

Okay here are the solutions for my problems here for the people having the same problem. 1 - First problem was I was unable to get the shortcode to work in multiple instances and that was because I used include_once at the shortcode function instead of include to include the separate php template. 2 - The second problem was that when I mapped my shortcode ...


0

This is untested, but you can try an EXISTS OR NOT EXISTS meta_query: $args = array( 'post_type' => array( 'post', 'event' ), 'posts_per_page' => '-1', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'event_start_date', 'compare' => 'EXISTS' ), array( ...


0

It is unlikely that you will achieve this as stated. WordPress has no concept of post subsets in a query, it only understands it as single set as results. If you don't need pagination you could just retrieve the set unsorted and re–sort it with PHP code. If you do need the pagination this would probably be extremely inconvenient and require massive custom ...


0

Put the following code in functions.php # +++++++++++++++++++++++++++++++Pagination ++++++++++++++++++++++++++++++++++++++ function kriesi_pagination($pages = '', $range = 2) { $prev = __('« Prev'); $next = __('Next »'); $showitems = ($range * 2)+1; global $paged; if(empty($paged)) $paged = 1; if($pages == '') { global ...


0

woa, you saved my day with this answer, i was trying to filter posts by custom numeric field called type_price and i was trying to do it that way: 'meta_query' => array( array( 'key' => 'type_price', 'value' => array( 0, 500000 ), 'compare' => 'BETWEEN', 'type' => 'NUMERIC', )), but it should be this way ...


0

I have figured out how to do this. It was somewhat close to what ankittiwaari suggested, but he didn't give a few key details. When you pass a variable under data in an ajax call, it automatically makes it a $_POST request assuming you are calling method: 'post' So I did what ankittiwaari said and created <input type="hidden" value="<?= ...


0

You are just confusing the array levels, ordering is not done inside the meta_query level but on the main query level. It is the difference between two classes: https://codex.wordpress.org/Class_Reference/WP_Query https://codex.wordpress.org/Class_Reference/WP_Meta_Query Also, as the meta query codex says: "Custom field value can be an array only when ...


2

If I understood this issue correctly the problem with your current code is that you are incrementing $totalPosts variable inside the while loop, so the variable gets incremented after every post thus resulting in the running order you described. To get the amount of posts returned matching the query, use $loop->found_posts (in this case) More ...


0

I would try it this way. Use a meta query to get your events list and load everything into a datatable. This seems like a perfect time to let visitiors filter and sort. Here's a simple implementtation (which may need tweaking). Just print or return the '$disp_data' variable from function call or in a template. If you don't have datatable set up I can help ...


1

Your best bet is not to run a bunch of queries but to loop through your data once to organize it then a second time to display it. Below is the logic you need. The exact code will depend on your data set. $events = array(); $posts = WP_Query( $args ); foreach ( $posts as $post ) { $venue = $post->venue; $day_of_month = date( 'd', $post->date ); ...


1

You can use the action pre_get_posts . Just make sure to check is_post_type_archive('question') and is_admin() to prevent affect queries in the whole website.


0

Assign the value of you $_POST variable to a html element or javascript variable in front-page.php like //html element <input type="hidden" value="<?= $_POST['variable']?>" id="post-variable"> or in javascript variable var postVariable = "<?= $_POST['variable']?>"; Now in ajax call, you can get the value as var myValue = ...


1

If you'd like to change the SQL query for a WP_Query, you're going to need to use the posts_join filter, or in your case, it's easier to go for a simple posts_where: function my_has_comments_filter( $where ) { $where .= ' AND comment_count > 0 '; return $where; } add_filter( 'posts_where', 'my_has_comments_filter' ); $query = new WP_Query( ...


2

You can get all posts by using a meta_query that gets posts where the key either does or does not exist, and then order on meta key with a secondary order for posts with same value or no key: $args = array( 'post_type' => 'my_product' 'tax_query' => array( array( 'taxonomy' => 'my_product_cat', 'field' => ...



Top 50 recent answers are included