Pre_get_posts comparison with custom field doesn’t work
PHP handles all content inside single quotes as string therefore your code does not work as expected. You should remove quotes from $today in meta_query and from true in suppress_filters
PHP handles all content inside single quotes as string therefore your code does not work as expected. You should remove quotes from $today in meta_query and from true in suppress_filters
I may have misinterpreted what you are asking here but you don’t need to do this using pre_get_posts(). Instead you could either add this to your functions file: function output_page_type() { global $post; $pagename = $post->post_name; return $pagename; } and then on the relevant page echo output_page_type(); to get the page title. Or you could … Read more
Not 100% sure if this will work, but have you tried querying the meta key in a seperate array inside the $args? For instance… $args = array( ‘category’ => 2042, ‘posts_per_page’ => 3, ‘orderby’ => ‘meta_value’, ‘meta_query’ => array ( array ( ‘key’ => ‘vacation_order’, ‘compare’ => ‘IN’ ) ) ); $new_query = new WP_Query( … Read more
I noticed that the problem was related that one custom field was a taxonomy, so, as I was using the archive page for that custom post type, I only needed to filter the query looking for the date limits. $fecha_actual = current_time(‘Ymt’); //t regresa el ultimo día del mes $fecha_anterior = date(‘Ymd’, strtotime(“first day of … Read more
Turns out that it is possible to run a WP_Query inside a pre_get_posts action keeping in mind two simple principles: pre_get_posts action will alter every query that you make, so you need to contain its action specifying which queries you need to alter. In this case, this containment is done with this line: if ( … Read more
Testing requested query in pre_get_posts
Well first off get_current_screen only works in admin area but even in admin can cause problems on some page. Use the $pagenow global variable instead. So now if you want to hide media not uploaded by a contributor this is how I would do it. // Prevents user to see all uploads, only theirs add_action(‘pre_get_posts’,’wpse_users_own_attachments’); … Read more
The reasons your pre_get_posts filter does not work on the widget are the condition !$wp_query->is_main_query() makes the filter handle only the main query and nothing else Your special parameter myattr is “self populating” only on the main query but it is not propagating automatically to other queries. To make it work on the widget, the … Read more
New custom post type entries are not sorted correctly in admin using pre_get_posts
You’re not actually using the $_GET[‘orderby’]-value anywhere. What you’re doing in your code is ordering by the text_date-meta value, always. If you want to order by date by default, and when $_GET[‘orderby’] is set to default orderby-values or if $_GET[‘orderby_meta’] is set, order by those instead you need to do something like this: if (isset($_GET[‘orderby’]){ … Read more