New answers tagged

-1

Use this code: $args = array( 'post_type' => 'food', 'orderby' => 'meta_value_num', 'meta_key' => 'listing_price', 'order' => 'DESC', 'posts_per_page' => $count, 'paged' => $paged, ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ): while ( $loop->have_posts() ...


0

You might want to check out the query improvements in WP 4.2 for ‘orderby’ and ‘meta_query’. Details are on https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query. Try to name your meta_query clauses, then order by them. Following code is untested: $query = array( 'order' => 'DESC', 'orderby' => '...


0

date() PHP functions returns the value of time() PHP functions in the specified format. time() use server local time. If you want to get date/time based of WordPress configuration, you could use current_time(), a WordPress function, instead of native PHP functions. // 'timestamp' = Unix Timestamp or 'U' PHP time format $current_time = current_time( '...


0

There is an interesting answer on SO which present different methods to resolve that issue: How do I query for posts by partial meta key? By custom SQL query (in my project I've used that solution) By changing the meta schema to store the dynamic meta_key as a meta_value By using the posts_where filter to update query behaviour based on input


0

Turns out what I needed is to use "in_category" NOT "is_category". Here is the solution: <?php if (in_category(7)) { echo 'yes'; } else { echo 'no'; } ?>


1

nopaging=true override posts_per_page and sets it to -1 which will return all posts. Remove that and you should be fine


1

Yep it's possible depending on what you want to cache. I've done a similar thing to cache the main loop on our homepage. Essentially you can use the posts_request and posts_results to hijack the query and hit the cache instead then also use found_posts to correct the pagination. Really rough example pulled from our code (untested) but you should help you ...


1

Use array_diff() to remove the current post ID from an array of other post IDs. $include = array( 17, 111, 108, 158); $include = array_diff( $include, array( $post->ID ) ); $args = array( 'post__in' => $include ); $posts = new WP_Query( $args ); Edit to include a point raised by Pieter Goosen--If $include is not a hard-coded array of IDs but ...


0

I rewrote the function based on a few other posts I have seen online and now have the following code which works without any errors. I am still open to any further suggestions on the best way to streamline this code even further it it can be. Thanks again function downloads_shortcode($atts){ extract(shortcode_atts(array( 'download_type' => '' ), $...


0

You are so very close. In your first line just replace get_option with get_theme_mod and you're done, provided you have already added a customizer control for posts_per_page_option in the usual way.


0

I also don't understand what rwmb_meta() does, but I can see a couple obvious problems in the code as written. Apparently, you want the foreach to deliver as many "posts" as it finds $image in $images, and echo a result each time. If so, you would remove the bracket from: $schedule_image_url = $image['url']; } and place it after each $image ...


3

REWORKED APPROACH The issue with the original answer is that although we pass post ID's to post__in parameter, being on the author page, the main query removes all posts that does not belong to the author. We can pass an empty string to author_name via the pre_get_posts filter, but that inherintly breaks the query object, which is a no-no. This calls for a ...


0

I don't know what rwmb_meta() does. But if rwmb_meta('tbf_scheduledatetime') returns array, then try something like this: ... $images = rwmb_meta( 'tbf_scheduleimage', 'type=image&size=full'); $dates = rwmb_meta('tbf_scheduledatetime'); var_dump( $dates ); ... You'll probably want to loop thru $dates and repeat the post data for each element in $...


1

Try changing hierarchical: false in your CPT definition (if your application allows). When CPT's are set to hierarchical: true all posts will be queried in the admin dashboard which can cause memory issues.


0

This is template_redirect Here : https://www.rarst.net/images/wordpress_core_load.png add_action('template_redirect', 'yourFunction');


0

From my point of view you are trying to make a pagination on a page, which can also mean wordpress is trashing your page/paged var, this can be easily solved by adding a filter adding a custom vars page/paged add_filter('query_vars', function( $vars ){ //!!SUPER IMPORTANT!! - always *APPEND* $vars array (NOT re-assign) $vars[] = 'page'; $vars[] ...


2

It will be made possible in 4.6 (assuming no changes till release) with the new posts_pre_query filter https://core.trac.wordpress.org/ticket/36687


0

TO get this solution to work. I simply enabled pretty permalinks. Incase you activate permalinks but you are getting server 404 error, this may imply that the .htaccess is not activated on your server. follow the steps below to get this sorted out. How to Activate an .htaccess file If you have access to the server settings you can edit the configuration to ...


9

At the moment, it is not possible. When 'pre_get_posts' runs, is too late to stop WP_Query to perform a query. WordPress itself, when you try to query a taxonomy that does not exists, adds AND (0 = 1) to the WHERE clause of the SQL query, to ensure it returns no results very quickly... There's a trac ticket with a patch that will probably lands in core ...


2

This is PHP question more than a WordPress question. As @Mark commented: returning from the action do not return by magic from the calling function That is true. Placing return in function mean exit the function and placing return in a PHP file mean exit the file. Do not get confused with PHP construct exit() :P (You might find a better answer on SO ...


0

The problem was that the CPT was set to hierarchical, switching this to hierarchical: false in the functions.php resolved the problem.


7

Not sure if you mean this kind of approach: add_filter( 'paginate_links', function( $link ) { return filter_input( INPUT_GET, 'from_expired' ) ? remove_query_arg( 'from_expired', $link ) : $link; } ); to remove the from_expired from the pagination links if it's in the current GET query.


4

As @Howdy_McGee suggested and I made a quick test. Removing key do the trick so you can just remove the key and add compare LIKE if you do not want exact match. Example:- $posts = new WP_Query(array( 'meta_query' => array( array( 'value' => 'meta_value' ) ) )); This will produce the SQL like WHERE 1=1 AND ( ...


0

Notice: caller_get_posts is deprecated since version 3.1! Use ignore_sticky_posts instead.


0

You don't need to alter the code for different formatting, you can simply use the :first-child pseudoclass in css. The foreach loop will, presumably, generate similar blocks of code, structured like this: <div class="all-my-posts"> <article class="my-slider-post"> ... </article> <article ..> </article> ....


0

You can use meta_query to combine multiple queries using either AND or OR relation. Granted, it does get a bit complex in this case, but because you can nest multiple queries, you can still achieve it. This is covered in the WP_Meta_Query documentation and also to an extent in the WP_Query docs. Here's what I think should work. I haven't tested this, but ...


0

I was working on someone else WP installation so I can get the info of WP Database to show Products information in a side-page. I've noticed that they have a plugin installed (Taxonomy order) that always return the terms posts in the same orden. Sorry for the confusion!


1

Something like this should work, where series is the taxonomy name and not a option within a taxonomy: $args = array( 'post_type' => 'my_post_type', 'tax_query' => array( array( 'taxonomy' => 'series', 'operator' => 'NOT_EXISTS', ), ), ); $query = new WP_Query( $args ); reference with plenty ...


1

The p parameter accepts a single integer value, not a string or array of integers. You would want to use the post__in parameter which allows for an array of post id's. Just a few notes here: You will need to check if you actually have a valid array of post ID's before passing it to post__in. If an empty array is passed, WP_Query fails epically in that it ...


1

Such condition is impossible to express in current WP API. You can use search for title/content matches and meta query for LIKE matches on post meta, but there is no way to establish OR relationship between the two. The closest API way would be two–stage query where you query IDs for two sets of results (one for search, one for meta), then query full data ...


1

This $_GET['variable'] is an array, as per your URL query string ?variable[]=value1&variable[]=value2, $_GET['variable'][0] and $_GET['variable'][1] should return those 2 key values Edit - after the discussion - making it dynamic $meta_query = array(); if ( ! empty( $_GET["variable"] ) ) { if ( is_array( $_GET["variable"] ) ) { $meta_query['...


-3

<?php // Display blog posts on any page @ http://m0n.co/l $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('cat=4&showposts=2' . '&paged='.$paged); while ($wp_query->have_posts()) : $wp_query->the_post(); ?> <div class="col-md-6 col-post-blog wow fadeInUp"> <div class="post-...


-1

As per the WP Query Codex page all you need to do is change your wp query to this: $wp_query = new WP_Query( array( 'cat' => $cat_id ) );


1

The function get_the_category is returning you an array of objects (see codex for more info), so if the post has more than one term assigned, then you are just outputting the first one. So I think that doing a foreach loop will solve your problem: foreach ($category as $cat) { if ($cat->slug != 'featured') { echo esc_html($cat->slug); ...


3

My preference is a special query var that acts as a flag to trigger your filter: function wpse_228386_maybe_batman( $clauses, $wp_query ) { if ( $wp_query->get( 'iamthebatman' ) ) { // Fight crime } return $clauses; } add_filter( 'posts_clauses', 'wpse_228386_maybe_batman', 10, 2 ); Now you can "activate" it anywhere you like, ...


0

yess SOLVED i modified add_filter( 'posts_where', 'find_by_title_filter', 10, 2 ); function find_by_title_filter( $where, &$wp_query ) { global $wpdb; if ( $find_title_arg = $wp_query->get( 'find_title' ) ) { $where .= " AND SUBSTR(" . $wpdb->posts . ".post_title,1,1) LIKE '%$find_title_arg%' "; } return $where; }


0

You can use custom filters in $args (not tested) add_filter( 'posts_where', 'find_by_title_filter', 10, 2 ); function find_by_title_filter( $where, &$wp_query ) { global $wpdb; if ( $find_title_arg = $wp_query->get( 'find_title' ) ) { $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $...


0

The pre_user_query action hook in WordPress will allow you alter the SQL statements in the WP_User_Query object before they are run against the database. Note, this is an action, not a filter, so there's no need to return the $user_query that gets passed in. add_action( 'pre_user_query', 'add_my_custom_queries' ); function add_my_custom_queries( $...


1

You need a custom query for that. Basically, just a query that pulls 1 result from the posts table where the post type is your custom post type: global $wpdb; // the name you gave to your custom post type $post_type = 'my_custom_post_type'; // this is the query string that we'll use with the $wpdb class // selecting from the posts table where the ...


4

One of the easier ways to do this is to just add the filter before your query and remove it after: add_filter( 'posts_orderby', 'filter_function' ); $something = new WP_Query( $args ); remove_filter( 'posts_orderby', 'filter_function' ); Then you can just add your function into functions.php as normal: /** * The below function filters something * ...


3

Based on what you're asking and what I am understanding there is the_modified_date() or if you just want the value get_the_modified_date(). If you ever want the creation date you could use post_date and post_date_gmt. Several questions on this: How to get post creation date? What is considered the post's creation date for wp_insert_post? If you are ...


0

Make sure that your wordpress url in the settings at your admin dashboard is starting with https://. It is not enough to simply open the website with the https link. Wordpress has to know that your website is ssl secured.


0

I think the sort is either alphabetical or numerical. There is no API way to provide more elaborate sorting rules to the query and messing with SQL for it would be likely impractical. Since you are retrieving complete set of posts (don't have to worry about pagination) the easiest way would be: Request no sort from query. Re–sort $the_query->posts set ...


0

Try to put your post slugs in an array for the post_name__in parameter: 'post_name__in' => [ 'berlin', 'frankfurt', 'munich', 'augsburg' ],


1

First you will need the slug of the current category: $cat = get_query_var('cat'); $currentcat = get_category ($cat); Then you can use the same loop. Just add the following to $query_args to limit the outcome to the current category: 'category_name' => $currentcat->slug,



Top 50 recent answers are included