In a plugin for displaying recent posts in your sidebar widget, how can we apply a filter to the plugin's functions.php so that it won't include the current page/post in the display?
The plugin author replied, before he entered a long silence: "You can add custom parameter to the rpwe_default_query_arguments filter. Just add exclude => get_the_ID() to the filter."
Is it here, that we add it?
// Allow plugins/themes developer to filter the default query.
$query = apply_filters( 'rpwe_default_query_arguments', $query );
How?
This is the plugin: https://wordpress.org/plugins/recent-posts-widget-extended/
I found some guidance that appears to be quite simple
but then it results in errors in my site (localhost) while trying to correct the syntax. => seems to be not correctly used.
This is what I have so far:
add_filter( 'rpwe_default_query_arguments', 'rpwe_exclude_current' );
function rpwe_exclude_current ( $query ) {
'exclude' => get_the_ID()
$posts = new WP_Query( $query );
return $posts;
}
Here is the answer that worked in my situation:
add_filter( 'rpwe_default_query_arguments', 'my_function_name' );
function my_function_name( $args ) {
if( is_singular() && !isset( $args['post__in'] ) )
$args['post__not_in'] = array( get_the_ID() );
return $args;
}
Here is the site where I found it.
Related
I have the CPT (Custom Post Type) "news".
In the "category" taxonomy I have "food" and "health".
The archive page is accessed via the url:
www.mysite.com/news
Is there any way to show "news" by categories? Ex:
www.mysite.com/food/news
www.mysite.com/health/news
Thank you all in advance.
In that link I found the following solution:
The solution for me had three parts. In my case the post type is called trainings.
Add 'rewrite' => array('slug' => 'trainings/%cat%') to the
register_post_type function.
Change the slug to have a dynamic
category. "Listen" to the new dynamic URL and load the appropriate template.
So here is how to change the permalink dynamically for a given post type. Add to functions.php:
function vx_soon_training_post_link( $post_link, $id = 0 ) {
$post = get_post( $id );
if ( is_object( $post ) ) {
$terms = wp_get_object_terms( $post->ID, 'training_cat' );
if ( $terms ) {
return str_replace( '%cat%', $terms[0]->slug, $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'vx_soon_training_post_link', 1, 3 );
...and this is how to load the appropriate template on the new dynamic URL. Add to functions.php:
function archive_rewrite_rules() {
add_rewrite_rule(
'^training/(.*)/(.*)/?$',
'index.php?post_type=trainings&name=$matches[2]',
'top'
);
//flush_rewrite_rules(); // use only once
}
add_action( 'init', 'archive_rewrite_rules' );
Thats it! Remember to refresh the permalinks by saving the permalinks again in de backend. Or use the flush_rewrite_rules() function.
But I have a question that I couldn't do there:
This part:
Add 'rewrite' => array('slug' => 'trainings/%cat%') to the register_post_type function.
where do I do that? Or is this already embedded in the later code?
I am wondering if you're able to help me with the following WordPress customization. We're using the WP Job Manager plugin (https://wpjobmanager.com/) and I'd need a little help with a slug/permalink edit.
In the documentation is an article available which explains the following: at the current situation links are generated as follows: domain.com/job/job-name. However, I need the following structure: domain.com/job-category/job-name.
Please check: https://wpjobmanager.com/document/tutorial-changing-the-job-slugpermalink/
The article explains this. Please check the code on: Example: Adding the category to the base URL. When I remove the 'job' in the following code, the job listings are working fine, but the rest of my website returns in a 404 error (also after saving the permalinks).
$args['rewrite']['slug'] = 'job/%category%';
To
$args['rewrite']['slug'] = '%category%';
Full code:
function job_listing_post_type_link( $permalink, $post ) {
// Abort if post is not a job
if ( $post->post_type !== 'job_listing' )
return $permalink;
// Abort early if the placeholder rewrite tag isn't in the generated URL
if ( false === strpos( $permalink, '%' ) )
return $permalink;
// Get the custom taxonomy terms in use by this post
$terms = wp_get_post_terms( $post->ID, 'job_listing_category', array( 'orderby' => 'parent', 'order' => 'ASC' ) );
if ( empty( $terms ) ) {
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
$job_listing_category = _x( 'uncat', 'slug' );
} else {
// Replace the placeholder rewrite tag with the first term's slug
$first_term = array_shift( $terms );
$job_listing_category = $first_term->slug;
}
$find = array(
'%category%'
);
$replace = array(
$job_listing_category
);
$replace = array_map( 'sanitize_title', $replace );
$permalink = str_replace( $find, $replace, $permalink );
return $permalink;
}
add_filter( 'post_type_link', 'job_listing_post_type_link', 10, 2 );
function change_job_listing_slug( $args ) {
$args['rewrite']['slug'] = 'job/%category%';
return $args;
}
add_filter( 'register_post_type_job_listing', 'change_job_listing_slug' );
Well, I was also stuck in some type of error once. But in my case, I was creating everything my self. So I change the template accordingly.
However, In you case, I think it relates to URL Re-Writing. Please check out the following links. I hope those will help.
http://www.wpbeginner.com/plugins/how-to-change-custom-post-type-permalinks-in-wordpress/
https://codex.wordpress.org/Rewrite_API/add_rewrite_rule
https://paulund.co.uk/rewrite-urls-wordpress
I know there are plenty of solutions on the internet about how to get WooCommerce products on sale, by doing a WP_Query. However, WooCommerce doesn't seem to work fully if it's WC_Query object is not populated. For example: filter or sorting
Both these templates call:
woocommerce_products_will_display()
Which check's to see if the page is a taxonomy page (obvious false if you're using your own custom template):
if ( ! is_product_taxonomy() ) return false;
This is an example of a simple solution if you just want the products: WooCommerce: Display ONLY on-sale products in Shop
So, I there seems to be a couple of issues I need to solve here:
1) How to tell WC that my "Sale" page is a taxonomy page? Is there some sort of trick I need to do to force it into a taxonomy?
2) How do I get get WC_Query filled with the sales query (rather than just the WP_Query)
I have plugins that depend on:
$woocommerce->query->layered_nav_product_ids
being populated.
Any help is appreciated!
Thanks!!!
Well woocommerce_products_will_display() is pluggable, meaning you can define it in your own functions.php (or site-specific plugin) and alter it, having it return true for your specific template/page.
I think it could stand for some tweaking and a filter.
EDIT
I played around with this a bit more. Typically changing the posts that you want to retrieve is done in the pre_get_posts hook. I took a look at what WooCommerce is doing. They are adding something to the pre_get_posts hook and calling their special query stuff from there.
But their special query stuff dies if you aren't on a WooCommerce page. So, it made me thing that maybe we could just call it ourselves from our own function. I put this together and coupled with a special page template for a page called "on-sale" (basically just a copy of the shop template), seems to show just the for sale items with proper sorting and pagination.
Your mileage may vary, but I hope it helps.
function kia_pre_get_posts( $q ){
// We only want to affect the main query
if ( ! $q->is_main_query() ) {
return;
}
// Fix for verbose page rules
if ( is_page('on-sale') ) {
$q->set( 'post_type', 'product' );
$q->set( 'page_id', '' );
$q->set( 'page', '' );
$q->set( 'pagename', '' );
$meta_query = array( array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>'
) );
$q->set( 'meta_query', $meta_query );
if ( isset( $q->query['paged'] ) ) {
$q->set( 'paged', $q->query['paged'] );
}
// Fix conditional Functions
$q->is_archive = true;
$q->is_post_type_archive = true;
$q->is_singular = false;
$q->is_page = false;
}
$wc_query = WC()->query;
$wc_query->product_query( $q );
if ( is_search() ) {
add_filter( 'posts_where', array( $wc_query, 'search_post_excerpt' ) );
add_filter( 'wp', array( $wc_query, 'remove_posts_where' ) );
}
add_filter( 'posts_where', array( $wc_query, 'exclude_protected_products' ) );
// We're on a shop page so queue the woocommerce_get_products_in_view function
add_action( 'wp', array( $wc_query, 'get_products_in_view' ), 2);
// And remove the pre_get_posts hook
$wc_query->remove_product_query();
}
add_action( 'pre_get_posts', 'kia_pre_get_posts' );
I set up a plugin that adds a custom post type and then brings in a bunch of dummy content with wp_insert_post on activation like so:
register_activation_hook( __FILE__, array( $this, 'activate' ) );
public function activate( $network_wide ) {
include 'dummycontent.php';
foreach ($add_posts_array as $post){
wp_insert_post( $post );
};
} // end activate
I would like to remove this content when the plugin is deactivated so I set up this function:
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
public function deactivate( $network_wide ) {
include 'dummycontent.php';
foreach($remove_posts_array as $array){
$page_name = $array["post_title"];
global $wpdb;
$page_name_id = $wpdb->get_results("SELECT ID FROM " . $wpdb->base_prefix . "posts WHERE post_title = '". $page_name ."'");
foreach($page_name_id as $page_name_id){
$page_name_id = $page_name_id->ID;
wp_delete_post( $page_name_id, true );
};
};
} // end deactivate
It works just fine. Except because the custom post type is created with the same plugin that these two functions are run through, the post type is removed before the posts themselves can be through wp_delete_post. When I test these functions out without the custom post type posts are added upon activation and removed upon deactivation. So I know the problem is with the post type. Does anyone know how to work around this?
Try something like this (YOUTPOSTTYPE is the name of your post type):
function deactivate () {
$args = array (
'post_type' => 'YOURPOSTTYPE',
'nopaging' => true
);
$query = new WP_Query ($args);
while ($query->have_posts ()) {
$query->the_post ();
$id = get_the_ID ();
wp_delete_post ($id, true);
}
wp_reset_postdata ();
}
It works in my plugin, it should works in your's. (This has been tested with WordPress 3.5.1).
wp_delete_post($ID, false) sends it to Trash. Only when you remove from Trash is a post really deleted. That's why it works with $force = true.
So it works as expected. First posts go to Trash, then they get actually deleted. Like Recycle Bin. Trace the post_status change to see when it hits the Trash if you want to do anything then. Otherwise wait for the delete.
Also delete content on uninstall and not on deactivate. Consider deactivating a plugin as pausing it and uninstalling it when you really want it gone.
Try this Function
function deactivate () {
$args = array(
'post_type' => 'POST_TYPE',
'posts_per_page' => - 1
);
if ( $posts = get_posts( $args ) ) {
foreach ( $posts as $post ) {
wp_delete_post( $post->ID, true );
}
}
}
I am using wordpress as our CMS for our companies website.
We have about 5-10 pages which we want to insert the same call to action content in.
How can I create a shortcode that will allow me to embed the content from a post into a page?
Thanks
Steven.
I haven't tested this, but you'll probably want something along these lines:
function create_call_to_action_shortcode( $atts ) {
$call_to_action_content = '';
$query = new WP_Query( array( 'p' => $post_id, 'no_found_rows' => true ) );
if( $query->have_posts() ) {
$query->the_post();
remove_filter( 'the_content', 'sharing_display', 19);
$call_to_action_content = apply_filters( 'the_content', get_the_content() );
add_filter( 'the_content', 'sharing_display', 19);
wp_reset_postdata();
}
return $call_to_action_content;
}
add_shortcode( 'call_to_action', 'create_call_to_action_shortcode' );`
In your pages ( or other posts, for that matter ), you can simply insert [call_to_action] in the page/post content.
You can find more information on shorcodes here. :)
Edit:
In order to remove sharing buttons from the post content, you need to call remove_filter( 'the_content', 'sharing_display', 19);. I've update the code above.