How select a specific query when setting offset?

If you only wish to effect the one, single query then just can pass the offset argument through the arguments array. $offset = 1; $ppp = 3; if ( $query->is_paged ) { $page_offset = $offset + ( ($query->query_vars[‘paged’]-1) * $ppp ); } blog_items = array( ‘post_type’=> ‘post’, ‘paged’ => $paged, ‘posts_per_page’=> $ppp, ‘status’ => ‘publish’, … Read more

Creating a custom search for a specific post type

To enable search on a custom post type archive, I use the pre_get_posts hook. Add this code to your theme functions.php file. Here is an example of enabling search on a post type called “resource”: // Enable resource search add_action(‘pre_get_posts’,’pk_enable_resource_search’, 1000); function pk_enable_resource_search( $query ) { if ( $query->is_archive(‘resource’) && $query->is_main_query() && ! is_admin() ) … Read more

How can I get the query that would be run for the archive page?

In each of your hooks & filters handlers, in addition to checking for archive conditions, check for a custom query var that you can then set for a custom/manual query. For example, where you might have: function wpse_223991_pre_get_posts( $wp_query ) { if ( $wp_query->is_main_query() && $wp_query->is_post_type_archive( ‘my_post_type’ ) ) { // Awesome custom stuff } … Read more

How to run a mysql query when admin updates user role?

You are going in wrong direction there is no way to get user role while admin change from back end. You can retrieve the role object for the current user by calling get_role(), but that’s an object rather than a string with the role name. function get_user_role($uid) { global $wpdb; $role = $wpdb->get_var(“SELECT meta_value FROM … Read more

Insert static element only once in query archive

Use the Index of the post currently being displayed to trigger when your custom content is rendered. current_post can be found on WP_Query from within the loop. <?php while( $archive_query->have_posts() ) : $archive_query->the_post(); ?> <article <?php post_class(); ?>> The Content </article> <?php if ( $archive_query->current_post === 2 ): ?> <div>Custom Content</div> <?php endif; ?> <?php … Read more

How to access a query twice in different template files?

You could cache the results for that page load. It should hit it in header.php, cache the object, and in index.php you can check availability. Header $custom_query = new WP_Query( $args ); if( $custom_query->have_posts() ) { // Cache Query before loop wp_cache_add( ‘custom_query’, $custom_query ); while( $custom_query->have_posts() ) { $custom_query->the_post(); // Do some stuff, like … Read more

Query total number of posts

How could I get the static total number of posts which wouldn’t change irrespective of what the user filters? You may be looking for wp_count_posts(): Codex: WP COUNT POSTS Example getting number of all published posts: function get_all_them_posts(){ $count_posts = wp_count_posts(); $published_posts = $count_posts->publish; return $published_posts; } In template: <?php echo get_all_them_posts(); ?> For Custom … Read more

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