New answers tagged

0

I found an older post showing what I was missing from my code, so it seems like I didn't do a good enough search to begin with. For anyone wondering I was missing 'compare' => 'LIKE' from the query, so the code would look like this: $args_post = array('post_type' => 'release', 'orderby' => '_custom', 'order' => 'ASC', 'posts_per_page' => -1, ...


0

A)You can store the url in an array. The index of the post currently being displayed is $current_post. So you can use it also as array index. $featureImage[$the_query_name->current_post] = wp_get_attachment_url(); B) You can use multiple queries on a page. See: https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops


0

Use get_categories first to get all the post categories to use in the form, then loop through them to display a multicheck input... function hierarchical_category_inputs ($cat, $indent) { $cats = get_categories('hide_empty=false&orderby=name&order=ASC&parent='.$cat); $indent++; if ($cats) { foreach ($cats as $cat) { ...


1

Use this: $query = new WP_Query( array( 'cat' => 4 ) ); Where 4 is the ID of the top-level category (Blackburn). This query will in clude subcats. Reference - WP_Query


1

If you want to change the order of an already existing query, you shouldn't create a new WP_Query object but instead use pre_get_posts to alter the existing query, which prevents multiple queries running unnecessarily. From the pre_get_posts docs: This hook is called after the query variable object is created, but before the actual query is run. ...


1

I currently have no posts set as stickies on the website. Which tells me that nothing should show up in the loop. Exactly where you are wrong when passing an empty array to post__in. WordPress has some silly bugs which has no proper workaround and will most probably stay active bugs for a very long time. This is one of them. When we pass a valid array ...


0

Try setting an array for meta_query instead, I do not think using just a string for it is the intended usage, but this may work instead in your case: 'meta_query' => array( array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS' ) )


0

After a long time I found a solution for this issue (thanks to franzblog). If you are using version 4.2 or higher, you need to add the following lines in your functions.php file: add_filter( 'redirect_canonical', 'custom_disable_redirect_canonical' ); function custom_disable_redirect_canonical( $redirect_url ) { if ( is_paged() && is_singular() ...


3

In addition to the other answers and the typo pointed out by @birgire, here is another approach. First, we will set the title as a hidden custom field, but we will first remove the words a, and and the. You can use the following code. You must remove this code completely as soon as you have loaded any page once. If you have a huge site with a ton of posts, ...


4

The Problem I think there's a typo in there: The name of the filter is posts_fields not post_fields. That could explain why the title2 field is unknown, because it's definition isn't added to the generated SQL string. Alternative - Single filter We can rewrite it to use only a single filter: add_filter( 'posts_orderby', function( $orderby, \WP_Query $q ...


5

An easier way may be to go through and change the permalink slug on those posts that need it (under the title on the post writing screen) and then just use that for ordering instead of the title. ie. use post_name not post_title for sorting... This would also mean that your permalink may be different if you use %postname% in your permalink structure, ...


0

Found the error: $args = array( "posts_per_page" => 12, "paged" => $paged, "post_type" => array( "event", "post" ) , "post_status" => "publish", "meta_key" => "_thumbnail_id", 'meta_query' => array( 'relation' => 'OR', array( ...


2

This Plugin gives you a list of posts for each Site already, but it does not exactly do what you want to do. Basically, it gives you a list of all posts in your network. What you need to do afterwards is sort them by site_id and create an output for that. I do not know if this is the best version - but as WP_Query returns an one-dimensional array, you need ...


-1

$args = array( 'posts_per_page' => 4, 'post_type' => array( 'page', 'news-articles', 'about-gorilla','involved' ), 'orderby' => 'meta_value_num', 'meta_key' => 'home_order', 'order' => 'ASC', 'meta_query' => array( ...


1

You should really read the documentation on meta_query's Here is what is allowed in a meta_query key (string) - Custom field key. value (string|array) - Custom field value. It can be an array only when compare is 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN'. You don't have to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress ...


0

You should not use any hook before wp_loaded unless it performs some initialization, or required for user authentication, therefor the earliest is wp_loaded


0

As @Milo pointed out in the comments above, custom post types and taxonomies are registered on init, so this would seem to be the earliest hook available that is guaranteed to work for all content.


0

Run one wp_query for post_per_page = 2 and get the IDs of these 2 posts in an array to be excluded in the next 3 posts needed <?php // The Query $next_args = array( 'post_type' => '<your_post_type>', 'post_status' => 'publish', 'posts_per_page'=>2, ...


3

You can use WP_Query's pagination paramters. However, using two queries to accomplish this objective is inefficient. A better solution it to use one query and alter your markup based on the value of WP_Query's $current_post property, which (when used in the loop) reflects the index of the post currently being processed within the current page of results.


1

To expand on the answer by Global: Paste this into your theme's functions.php: function get_stations_query( $terms ) { //create new query object $query = new WP_Query( array( 'post_type' => 'stations', 'post_status' => 'publish', 'posts_per_page' => -1, ...


0

Create a new function, put all the code in it and replace 'pop' with $genre. Then call it like this: get_stations_query( $genre ); the return of your function should be return $query;


2

Your argument structure is not correct. It should be like [meta_query] => Array ( [0] => Array ( [key] => state [value] => a [compare] => LIKE ) [1] => Array ( [key] => state [value] => b ...


1

In case anyone else needs the code, this is how I solved it. @pieter-goosen was right, I did need to query the correct posts, but the real trick was matching the date format from the jquery datepicker when running the post query. Check the date formats below, jquery is 'yy-mm-dd', and the php equivalent is 'Y-m-d'. So the js for the datepicker was: ...


7

WP_Query::reset_postdata() sets the global post variable ($GLOBALS['post'] === $post) to the current post in the loop of the current WP_Query instance. public function reset_postdata() { if ( ! empty( $this->post ) ) { $GLOBALS['post'] = $this->post; $this->setup_postdata( $this->post ); } } What that means is that, if ...


0

Look in the query.php file. The class is there.


0

You can use the following code: global $post; $myposts = get_posts( 'numberposts=1&category=1' ); foreach( $myposts as $post ) : the_title(); the_content(); endforeach;


3

Something like this might work: $args = array( 'post_type' => 'post', 'tax_query' => array( array( 'taxonomy' => 'your_custom_taxonomy', 'operator' => 'EXISTS' ), ), ); $query = new WP_Query( $args ); You are basically asking for any post assigned to any term within your_custom_taxonomy.


0

You can handle this by define DATEDIFF() in SELECT For example: SELECT RECID, DATEDIFF(t1.qr_enddate, NOW()) AS diffORD FROM `mytablw` t1 ORDER BY diffORD ASC; This should work but in your case I strongly recommend to use wordpress default Filter API posts orderby. This filter is applied before a post-retrieving SQL statement is executed. Use it to make ...


1

There are few issue with your code When using WP_Query in conjuction with the_post() or using get_posts() in conjuction with setup_postdata( $post ), you need to reset the $post global with wp_reset_postdata(), not wp_reset_query(). wp_reset_query() is used in conjuction with query_posts() which you should never ever use You would want to reset posdata ...


1

preg_replace to the one-call rescue. /\w+/ will match words, while the third argument of preg_replace() will specify the number of matches. Since you want to remove them then we just pass an empty string as the replacement. $str = 'These are some words. But the first two will not remain.'; // pattern, replacement, string, limit echo preg_replace( '/\w+/', ...


0

Have you tried this? 'meta_query' => array( 'relation' => 'AND', array( 'relation' => 'OR', array( 'key' => 'start_date', /// or 'end_date' for cpt 'group_of_events?? 'compare' => 'BETWEEN', 'type' => 'DATE', 'value' => array($filter_start_date, ...


3

A more reliable way would be to filter the excerpt and to explode the string into an array, remove the first two key/value pairs from the array and then return your string add_filter( 'wp_trim_excerpt', function ( $text ) { // Make sure we have a text if ( !$text ) return $text; $text = ltrim( $text ); $text_as_array ...


0

I used substr to remove the first 21 characters from the string in the end. This was more consistent. $str = get_the_excerpt(); $str2 = substr($str, 21); echo str2;


-2

You can use get_the_excerpt() and then apply a left trim. $str = 'Hello World. This is another Hello World.'; echo ltrim($str, "Hello World."); // Output: This is another Hello World.


1

Try placing $myquery->the_post(); inside your while loop. I think get_the_ID() and get_the_title() depend on that being setup first. Also, wp_reset_query(); should be replaced with wp_reset_postdata();.


0

I hope this would do, i have not tested it. Test it on your own risk, but the query related to it very closely. function kv_get_custom_search_query($search_text){ global $wpdb; $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_title='%$search_text%' OR post_content='%$search_text%' OR post_title='%$search_text%' LEFT JOIN ...


0

Yes you need some arguments defined. A good place to easily get your loops defined is: https://generatewp.com/wp_query/ Here's an example of some arguments used. // WP_Query arguments $args = array ( 'post_type' => array( 'post' ), 'nopaging' => true, 'posts_per_page' => '3', ...


0

You only have one capture group, the ID is in matches[1], there is no matches[2]. EDIT- Here is the working version I tested in the 2016 theme. I created a page with slug sign-up, added the following to functions.php, then visited the Permalinks Settings page to flush rewrite rules. The value passed to product is available in the template via ...


4

If you need to: page the query retain 12 posts per page instead of "sticking" the desired posts on top of the required 12 only need to show those posts on the first page you can try the following $ids_args = [ 'post_type' => 'products' 'posts_per_page' => -1, 'orderby' => 'meta_value_num', 'meta_key' => ...


4

We could do that with two queries: First we fetch the post ids we need with the first query and then we merge it with our sticky post ids and feed it into the second query, using the post__in parameter for filtering and ordering: $args = [ 'post_type' => 'products' 'posts_per_page' => 12, 'orderby' => ...


0

there is no need to handle the pagination like this: $paged = get_query_var( 'paged', 1 ); Just put this after the loop: get_the_posts_pagination( 'mid_size' => 1, 'prev_text' => '&nbsp;', 'next_text' => '&nbsp;', 'screen_reader_text' => 'A' ) like this: if( have_posts() ){ ...


0

That would be nice if you query all three post_types at the same time, using one query, and then manipulating it using php. Consider this query: $args = array( 'post_type' => array( 'pt1', 'pt2', 'pt3' ), 'meta_key' => BUMP!!!! ); You can not use an array of meta_keys. So, I think the only way is to use wp_db object and make your own ...


4

Use pre_get_posts to alter the main query before it is run. The action runs on every query, use the Conditional Tags to target specific queries. function wpd_sort_by_meta( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'meta_key', 'your_meta_key' ); $query->set( 'orderby', ...


2

It might be possible to achieve this by using the post clauses filters and rewriting the generated SQL query. You can also run 3 queries, One very lean query to get the desired posts from the date_query One very lean query to get all the posts from the meta_query One final query to get the complete query object. This help specially with pagination if you ...


0

Your answer is perfect working in the first array lvl, for example: $args['meta_query'][] = array( 'key' => 'tour_itinerario_ciudades_repeater_%_tour_ciudades_nombre', '_key_compare' => 'LIKE', 'value' => 'MEXICO', 'compare' => 'LIKE', ); I need do some modifications for work in the second lvl in the array: $args['meta_query'][] = ...


0

Add this two lines to your theme's function.php file and everything will get back to work: add_filter(‘redirect_canonical’,’custom_disable_redirect_canonical’); function custom_disable_redirect_canonical($redirect_url) {if (is_paged() && is_singular()) $redirect_url = false; return $redirect_url; }


0

$the_query->the_post() is what increments the counter and loads the next post's data. You can also explicitly set the internal counter to an arbitrary number and begin incrementing from there... $the_query->current_post = 3; $the_query->the_post(); the_title(); // this will output the 4th post's title


1

To answer your question, get_queried_object and get_queried_object_id will give you all the info you need about most types of pages. However- if you're creating a new query in the template to change some query parameters, then you should instead be using pre_get_posts to alter the main query before it's run. Then you can just run the regular loop in your ...


0

This is the only option I have found: $current_category = single_cat_title("", false);


1

You're actually doing this in a way that is far more complicated than necessary. The foreach is actually excessive. What you want to do is something more like this: $args = array( 'posts_per_page' => '-1', 'post_type' => 'post', 'post_status' => 'publish', 'category__in' => $quicksand_categories ); $query = ...



Top 50 recent answers are included