Adding a custom query argument to a custom taxonomy term page - wordpress

I have a custom taxonomy created and I'm trying to add a new query argument with add_query_arg(), but it's not recognizing it at all when I visit the URL of a term of that taxonomy, with a query in the URL
for example, for a playlist taxonomy and the "edm" term and this URL:
http://someurl.com/playlist/edm/?some_variable=abc
some_variable is not recognized at all
When I use this code, to show all the query vars:
global $wp_query;
print_r($wp_query->query_vars);
some_variable doesn't exist, at all. It's like it doesn't see it at all!

You want to implement a hook somewhere in your plugin or theme so that WP is aware of your new variable.
public function my_plugin_add_query_vars($vars) {
$vars[] = "some_variable";
return $vars;
}

Related

WordPress/WooCommerce - remove wp_count_posts call in wp-admin

There's a call to wp_count_posts() on every page in wp-admin. I would think it should only happen on the product pages. Is there a way to disable the call on all pages but products? Our site has over 100,000 products, and this call slows down wp-admin pages.
The following is the caller log from Query Monitor
wp_count_posts()
wp-includes/post.php:2859
WC_Install::is_new_install()
wp-content/plugins/woocommerce/includes/class-wc-install.php:399
WC_Admin_Notices::init()
wp-content/plugins/woocommerce/includes/admin/class-wc-admin-notices.php:58
WC_Admin->includes()
wp-content/plugins/woocommerce/includes/admin/class-wc-admin.php:62
do_action('init')
wp-includes/plugin.php:470
Could you try the following. Add following function in your theme functions.php . All credits goes to - wordpress remove post status count from cms
I have edited $post_type where we want all post types and $exclude_post_types changed to product over page post type.
add_filter('bulk_post_updated_messages', 'suppress_counts', 10, 2);
// We need to let the function "pass through" the intended filter content, so accept the variable $bulk_messages
function suppress_counts($bulk_messages) {
// If the GET "post_type" is not set, then it's the "posts" type
$post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : '';
// List any post types you would like to KEEP counts for in this array
$exclude_post_types = array('product');
// Global in the variable so we can modify it
global $locked_post_status;
// If the post type is not in the "Exclude" list, then set the $locked variable
if ( ! in_array($post_type, $exclude_post_types)) {
$locked_post_status = TRUE;
}
// Don't forget to return this so the filtered content still displays!
return $bulk_messages;
}

Using cpt and pass a variable using url

I want to create a nice readable permalink structure for my custom post type (CPT). My CPT "movie" has the following rewrite-slug movie/movie_name" (all works fine).
Now i want to add arg like this: movie/movie_name/arg and use arg in my template file as a php variable.
But obvious it lead to not-found-page. How can i achieve this target?
edit: i want it in FRIENDLY URL format, it means i dont want to use GET for this.
You may pass it like movie/movie_name?movie_arg=movie_value. It is will be available with $_GET['movie_arg']. Of course your need extra sanitization to handle this data.
To be able to read this in a WordPress way add params to a query_vars filter
function add_movie_arg_to_query_vars( $qvars ) {
$qvars[] = 'movie_arg';
return $qvars;
}
add_filter( 'query_vars', 'add_movie_arg_to_query_vars' );
Note: it should not be same as reserved WordPress query parameters
This way it will be available at your template with get_query_var('movie_arg')
print_r( get_query_var('movie_arg') ) // movie_value
More information here

Modify the product search query

I have replaced the wordpress search engine with the WooCommerce product search engine, that is, to include it in the header of my site I use:
get_product_search_form();
instead of :
get_search_form (true);
I would like the product search engine to also search the product categories.
The problem is that I have a category called "Jackets" but the product in its title and in its content uses the word without the "s" so that if I search for the word "jacket" I get many results, but if I search the word "jackets" (with "s") I do not get any, despite that being the name of the category.
I have thought about modifying the query that makes the request so that it adds something of the type: OR category_name LIKE $ search_term, and I tried to use pre_get_posts in functions.php to modify the query, but it doesn't work.
Could someone help me modify that search query for products so that I can also return the products that are of the category whose name matches with the search term.
Thank you very much in advance!
Kind regards,
Raquel
You can look at this link .
simple example:
// hook into wp pre_get_posts
add_action('pre_get_posts', 'jc_woo_search_pre_get_posts');
function jc_woo_search_pre_get_posts($q){
if ( is_search() ) {
add_filter( 'posts_join', 'jc_search_post_join' );
add_filter( 'posts_where', 'jc_search_post_excerpt' );
}
}

Can't get post ID and category using wp_insert_post_data

I would like to create post titles automatically. The post title is made from the category and category ID before the post is created in the database but does not get the category and ID.
Any help will be much appreciated, Thank You.
function my_post_titles($data, $postarr){
$postid = $postarr['ID'];
$postcategory = $postarr['post_category'];
$data['post_title'] = $postcategory."-".$postid;
return $data;
}
add_filter('wp_insert_post_data', 'my_post_titles', '99', 2 );
Couple of things to be aware of here: wp_insert_post_data is the filter used by wp_insert_post to allow changes to its first parameter, so to understand your problem, you should check how wp_insert_post works.
https://developer.wordpress.org/reference/functions/wp_insert_post/
Essentially, if the post you are creating a new post, then it should not have any ID (as it's not even created and saved to the database yet). To have access to the post ID, I suggest you use save_post hook rather than wp_insert_post. save_post is the action triggered after the post is created or updated. For example:
add_action('save_post', function($post_id) {
$title = get_the_title($post_id);
if ($title) {
return; // if the post has already has a title, then do nothing
}
// Otherwise, update the post title here
});
Also, $postarr['post_category'] is an array, not a string, so to get the correct information, you must convert it to string before concat it with the post_id.

WP Shortcodes not being added

I was creating a wordpress plugin where the user enters in some information, and is able to generate shortcodes. I was not quite sure where the shortcodes are supposed to go - my current setup is class-based, and I want to be able to create a shortcode when an AJAX request is being made, and is successful. The following two methods are in the same file in the class.
This method gets called via the admin-ajax.php file:
public static function processAjax()
{
global $wpdb;
$event_data = $_POST['obj'];
$event_title = $event_data[0];
$event_subdomain = $event_data[1];
$result_events = $wpdb->get_results("SELECT * FROM wp_shortcode_plugin WHERE subdomain = '{$event_subdomain}'", OBJECT);
if (sizeof($result_events)>0) {
echo "duplicate";
} else {
add_shortcode($event_subdomain, 'getEmbed');
$results = $wpdb->insert('wp_shortcode_plugin', array("event_name"=>$event_title, "subdomain"=>$event_subdomain));
echo json_encode($_POST['obj']);
}
die();
}
And here is my getEmbed() method that I would like to call.
public static function getEmbed()
{
return 'test';
}
It seems that the shortcodes are not being created, however. Am I missing something here? Also, is it possible to pass a value to the getEmbed function from the add_shortcode() method?
Instead of adding shortcode directly from AJAX, you should use update_option to store in the information for the shortcode to be loaded. If option doesn't exist, it will be created.
Than you will simple use wp_init hook to load up all of the shortcodes you need to load in the function.php file for the theme or plugin php file.
You should use get_option within the wp_init hook and check the values in there. You will need to have function(s) associated with the shortcodes, which can be autogenerated in php using create_function or you can route them through 1 function (defined in your php file) that will have the $atts and $content parameters defined and do whatever depending on the value of your get_option that you send to that function.
add_shortcode function should be defined within the wp_init hook, after checking the value of the get_option function. You will need to give your option a name and add to it via the ajax function. the option will most likely want to be an array, that wordpress will automatically serialize. Than you use that array returned from get_option to loop through the array of shortcodes, and call add_shortcode as many times as you need there. This requires setting up your option array so that it has a shortcode tag defined in each index of the array. I would, personally, make the shortcode tag the key of the array and all attributes of the shortcode, imo, would than be an array of that array.
Hope this helps you to get started on this.

Resources