When add_query_arg() is necessary?

The difference is, in “plugin_part1-fixed.php” you wouldn’t lose existing args. Consider the url: https://www.example.com/page/?foo=bar Your code in “plugin_part1.php” would output <a href=”https://www.example.com/page/?custom_var=column”>text</a> Note, the existing arg “foo” has been lost, the code in “plugin_part1-fixed.php” would output: <a href=”https://www.example.com/page/?foo=bar&custom_var=column”>text</a> Note, the existing arg “foo” is still present and your arg has been appended. Since you don’t … Read more

How to extract all ID variables from a query string?

$string will contain a comma separated list of IDs: $ids = array(); foreach( $array as $post ) { if( !in_array( $post->ID, $ids ) ) { $ids[] = $post->ID; } } $string = implode( ‘, ‘, $ids ); Note: This uses basically no wordpress, other than the post object…which is really just an object.

is_archive() doesn’t work on public query var archive pages?

Try this: add_action( ‘template_redirect’, ‘my_test_if_archive’ ); function my_test_if_archive() { global $wp_query; $qv = array_keys( $wp_query->query ); $archives = array(‘year’, ‘monthnum’, ‘day’, ‘w’, ‘m’, ‘author’, ‘post_type’); $is_archive = ! empty( array_intersect( $qv, $archives ) ); $is_tax = ! empty( $wp_query->tax_query->queries ); if ( $is_archive || $is_tax ) { // this is an archive } }

$query->query_var[‘post_type’] not set for pages

To clear up some confusion in previous answers, pre_get_posts isn’t a filter so you don’t need to return anything. Your only problem that I see is the if: if ( !is_search() && !in_array( get_post_type(), $post_types ) ) return; Basically get_post_type() is going to return false during pre_get_posts because the global $post hasn’t been set yet … Read more

Can’t pass table to $wpdb->prepare

The prepare() method escapes %s. The second piece of code you listed breaks because quotation marks are added to the table name, hence it doesn’t match what’s in the DB. The first piece of code works because it’s a straight string replacement hence matching the name of the table in the database. What is the … Read more

Count user posts by user ID, Post type and Post status

Here is a quick solution to get the post count any kind of filtering you want function custom_get_user_posts_count($user_id,$args ); $args[‘author’] = $user; $args[‘fields’] = ‘ids’; $ps = get_posts($args); return count($ps); } Since this function uses get_posts you can filter and use anything that you can with WP_Query So in your case you can use it … Read more

add_query_arg() XSS Vulnerability

Unless I’m missing something that is staring me in the face, you aren’t using add_query_arg() or remove_query_arg(); since those are the only functions affected by this particular exploit you should be safe. Your code does use the query_vars filter and get_query_var() but neither of those are effected by the exploit you’ve referenced. Otherwise your code … Read more

Categories and products in random order

as suggested by kaiser, my last working edit as solution: ok, inspired by @s_ha_dum answer i manged to get a solution: //this 1. part is out of the woocommerce-template.php and part of the woocommerce_product_subcategories() $product_cat = get_term_by( ‘slug’, $product_cat_slug, ‘product_cat’ ); $product_category_parent = $product_cat->term_id; $pcat_args = array( ‘child_of’ => $product_category_parent, ‘hide_empty’ => 1, ‘hierarchical’ => … Read more

Get a user’s most recent post title

You just set the ‘author’ parameter in a WP_Query query or get_posts (which accepts the same parameters): $recent = get_posts(array( ‘author’=>1, ‘orderby’=>’date’, ‘order’=>’desc’, ‘numberposts’=>1 )); if( $recent ){ $title = get_the_title($recent[0]->ID); }else{ //No published posts } (Note the ‘orderby’ and ‘order’ here are redundant because they are set to their default values, but you get … Read more