Using pre_get_posts to target a query in the sidebar
I think you can achieve this by adding a query var. And then you can use something in pre_get_posts like this: if ( get_query_var( ‘your_var’ ) ) { // do something }
I think you can achieve this by adding a query var. And then you can use something in pre_get_posts like this: if ( get_query_var( ‘your_var’ ) ) { // do something }
The pre_get_posts filter is the right place to go. You can check if you are on a search query, and alter it the way you want. Here’s an example: function search_filter($query) { if ( !is_admin() && $query->is_main_query() ) { // Check if we are on a search page if ($query->is_search) { // Get the search … Read more
You need to look at the documentation for the function, which is available here. The function takes the following arguments: $table The table name. $data A PHP array where the keys are the columns and the values are the the values to be inserted into those columns. $where A PHP array where the key is … Read more
You can’t easily query inside arrays inside a meta value. This is because there’s no such thing as an array value in MySQL. Instead the value is stored as a serialized string. So an array like this: array( 0 => 29, 1 => 28, ); Is stored in the database as this string: a:2:{i:0;i:29;i:1;i:28;} So … Read more
That is a placeholder escape string generated by wpdb::placeholder_escape() which is used by wpdb::add_placeholder_escape() and which is called by wpdb::prepare(). So it is safe to keep those escape strings, but there is a wpdb method for removing them: wpdb::remove_placeholder_escape(): // In your case, you’d use $this->wpdb in place of $wpdb. $query = $wpdb->add_placeholder_escape( ‘LIKE %night%’ … Read more
I would bet my bottom $ that the permalinks are the problem, your link shows a heck of a lot wp_rewrite queries.. take a look at this a similar issue
Check if this solution work for your case: add_filter(‘posts_request’, ‘supress_main_query’, 10, 2); function supress_main_query( $request, $query ){ if( $query->is_main_query() && ! $query->is_admin ) return false; else return $request; } posts_request is the last filter called before running the query, and pass to you the $requestvariable with the generated SQL string and $query, with the WP_query … Read more
An easy way is to use a static variable to store author name, then run again same function on a later hook, maybe ‘posts_results’ and set back the variable: function wpquery( $query, $query_on_results = NULL ) { static $author_name = NULL; if ( current_filter() === ‘pre_get_posts’ ) { if ( $query->is_author() && $query->is_main_query() ) { … Read more
Use like this query_posts( array( ‘category__in’ => array(5,1), ‘posts_per_page’ => 1, ‘orderby’ => ‘title’, ‘order’ => ‘ASC’ ) ); or $my_query = new WP_query(array(‘category__and’ => array(5,1))); while ($my_query->have_posts()) : $my_query->the_post();
You are correct, you will need to get the image URL and set is as a variable so you can echo it out as an inline style in your HTML. I’ve updated your foreach loop below and set the image as an inline style of your <li> element: foreach( $postQuery as $post ) : setup_postdata($post); … Read more