Change Query String to pretty permalink

Try this rewrite rule in your functions.php function string_url_rewrite() { global $wp_rewrite; add_rewrite_tag(‘%pet-page%’, ‘([^&]+)’); //In the rewrite rule you need to enter page slug and page id add_rewrite_rule(‘^Enter_Page_slug/([0-9]+)/?’, ‘index.php?page_id=Enter_Page_ID&pet-page=$matches[1]’, ‘top’); $wp_rewrite->flush_rules(true); } add_action(‘init’, ‘string_url_rewrite’, 10, 0); Now you also need to change the format of URL in anchor tag. For example, your previous URL http://www.example.com/my-account/view-pet/?pet-page=2 … Read more

How to detect custom query inside `posts_where` hook?

If we have the query arguments like this $args = array( ‘queryid’ => ‘blackjack’, … ); $q = new WP_Query( $args ); Note we added one custom query argument that is not officially present in here. Now we can identify the exact query by that queryid if we use the second parameter of the posts_where … Read more

Get posts by category with pure SQL query

If you want to be able to get the category by name, this should work: SELECT DISTINCT substr(post_title, – instr(reverse(post_title), “https://wordpress.stackexchange.com/”) + 1) AS year FROM {$wpdb->posts} p LEFT JOIN {$wpdb->term_relationships} rel ON rel.object_id = p.ID LEFT JOIN {$wpdb->term_taxonomy} tax ON tax.term_taxonomy_id = rel.term_taxonomy_id LEFT JOIN {$wpdb->terms} t ON t.term_id = tax.term_id WHERE post_status=”publish” AND … Read more

WP database error for comments_popup_link()

Your template code includes $wpdb->print_error(). This function prints the last database error between [ and ] brackets, and the executed SQL code. But if there is no error, you just see the empty brackets and the SQL. $wpdb->show_errors() is used to enable displaying of database errors. If you want to see all database errors, you … Read more

WordPress custom archive page

Of course it breaks. Pagination depends on the main query saved in the $wp_query object and you have overwritten that. When the next page load the ordinary wp_query that you have not overwritten will run and it won’t match the one you’ve altered. Create a filter for pre_get_posts to alter the main query. function pregp_wpse_97354($qry) … Read more

How can I display a specific user’s first published post?

The following query retrieves the oldest post of a specified user/author: $user_id = 42; // or whatever it is $args = array( ‘posts_per_page’ => 1, ‘post_status’ => ‘publish’, ‘author’ => $user_id, ‘orderby’ => ‘date’, ‘order’ => ‘ASC’, ); $first_post = new WP_Query($args); if ($first_post->have_posts()) { $first_post->the_post(); // Now you can use `the_title();` etc. wp_reset_postdata(); } … Read more