Group WP_Query by category

You could look at modifying the WP_Query with a SQL command to group them, but that’s a bit beyond my current MySQL, however, I’ve always done it by running a foreach on the taxonomy itself with this http://codex.wordpress.org/Function_Reference/get_categories Here’s some sample code: <?php global $post; $current = get_the_ID($post->ID); $cargs = array( ‘child_of’ => 0, ‘orderby’ … Read more

How to get posts from two categories with WP_Query?

In my experience using ‘posts_*’ filters (‘posts_request’, ‘posts_where’…) in combination with str_replace / preg_replace is unreliable and unflexible: Unreliable because if another filter modify uses one of that filters, in better case one gets unexpected results, in worst cases one gets SQL errors. Unflexible because changing an argument, e.g. ‘include_children’ for categories, or reuse the … Read more

How to add multiple custom URL variables?

I pretty sure this filter lets you add an array of variables. I’ve not tested this: function add_custom_query_vars( $vars ){ $vars[] = “variable1”; $vars[] = “variable2”; $vars[] = “variable3”; //… etc return $vars; } add_filter( ‘query_vars’, ‘add_custom_query_vars’ ); Or another way of doing it would be to do this: function add_custom_query_vars( $vars ){ array_push($vars, “variable1”, … Read more

How to check if I’m on a custom post type archive in the admin area

Here are three possibilities, based on the /wp-admin/admin.php file: Method #1 There are the global $pagenow and $typenow available: global $pagenow, $typenow; if( ‘my_venue’ === $typenow && ‘edit.php’ === $pagenow ) { // … } Method #2 Then there’s the current screen, also set in the admin.php file with set_current_screen(); $screen = get_current_screen(); if( is_a( … Read more

Order by Category and Post in WP custom Query

To do this you have first get all the category in ascending order by get_categories then you have to pass the cat_id in WP_Query to get the post related with that category. $args_cat = [ ‘orderby’ => ‘name’, ‘order’ => ‘ASC’, ‘hide_empty’ => 0, ]; $categories = get_categories($args_cat); //print_r($categories); if (!empty($categories)): foreach ($categories as $category): … Read more

What to do in this case after wp_query has been modified

If the code shown above is the one the plugin is using, it will affect every query, since it’s not checking for is_main_query() (https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts) What you could do, is remove the action, do your query, and then add the action again (if needed) remove_action( ‘pre_get_posts’, ‘category_and_tag_archives’ ); $myQuery = new WP_Query($queryArgs); add_action( ‘pre_get_posts’, ‘category_and_tag_archives’ ); … Read more

Where should you reset postdata?

Short version: As Tom J Nowell said, You shouldn’t cleanup if there’s nothing to clean Long version: However, if you put wp_reset_postdata() after (or outside) the loop, it would still work perfectly fine. I have used the function in various scenarios, including something like this: dynamic_sidebar( ‘main-sidebar’ ); wp_reset_postdata(); The reason was that some widget … Read more

paginate_links ignore my format

This part: ‘base’ => str_replace( 999999999, ‘%#%’, esc_url( get_pagenum_link( 999999999 ) ) ), is generating a page part like this one: ‘base’ => http://example.tld/page/%#%/ If we peek into paginate_links() we see the default: ‘base’ => $pagenum_link, // http://example.com/all_posts.php%_% : // %_% is replaced by format (below) ‘format’ => $format, // ?page=%#% : %#% is replaced … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)