Help with plugin adaptation
-
Hello,
this is very good plugin, but need to adapt to my specification.
I need filter related posts by specific field (datetime) and show only this one which are actual.
My current template has a widget which shows as above but it is not possible to relate within other posts.
I wonder if you could help me to merge these two functionalities to your plugin.
I can offer you to translate po file to Polish version
-
I need filter related posts by specific field (datetime) and show only this one which are actual.
Can you explain a little bit more what you mean by that.
OK. I will try. I have for example post types: Persons and Events.
These posts are related by the same category.
Event has field: Date of Event.And on widget I want to see related events to person but Date of Event >= Today.
Search for both post types in the widget. With this in your (child) theme’s functions the widget will only return events if the current post is from the persons post type.
๏ปฟadd_filter( 'related_posts_by_taxonomy', 'rpbt_filter_posts', 10, 4 ); function rpbt_filter_posts( $results, $post_id, $taxonomies, $args ) { // Custom post types names $persons = 'persons'; $events = 'events'; $post_type = get_post_type( $post_id ); if ( $post_type !== $persons ) { // Don't return anything if the current post is not the 'persons' post type return array(); } $events = array(); foreach ( $results as $result ) { if ( $events === $result->post_type ) { $events[] = $result; } } // return events only return $events; } add_filter( 'related_posts_by_taxonomy_posts_clauses', 'rpbt_post_where', 10, 4 ); function rpbt_post_where( $pieces, $post_id, $taxonomies, $args ) { global $wpdb; // meta key $date_meta_key = 'date'; // Custom post type $persons = 'persons'; $post_type = get_post_type( $post_id ); if ( $post_type === $persons ) { // add events sql to query $date = date( "Y-m-d" ); $pieces['join_sql'] .= " INNER JOIN $wpdb->postmeta AS mt3 ON ( $wpdb->posts.ID = mt3.post_id ) INNER JOIN $wpdb->postmeta AS mt4 ON ( $wpdb->posts.ID = mt4.post_id )"; $pieces['where_sql'] .= " AND ( mt3.meta_key = '$date_meta_key' AND ( ( mt4.meta_key = '$date_meta_key' AND CAST(mt4.meta_value AS DATE) >= '$date' ) ) )"; $pieces['order_by_sql'] = "mt3.meta_value ASC"; } return $pieces; }Change these to the correct values:
// Custom post types names $persons = 'persons'; $events = 'events'; // meta key $date_meta_key = 'date'; // Custom post type $persons = 'persons';btw:
consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above.Thank you! I will try to adapt in coming days.
As I told you I would be happy if I could translate po file. How can I deliver you these files?Hi,
I used your functions but it doesn’t show on widget anything.
I check SQL code with Query Monitor (https://ww.wp.xz.cn/plugins/query-monitor/) and in PHP MyAdmin shows correct records. But not show it on widget..
Should I enable any setting?SELECT staging_rnpl_posts.* , count(distinct tt.term_taxonomy_id) as termcount
FROM staging_rnpl_posts
INNER JOIN staging_rnpl_term_relationships tr
ON (staging_rnpl_posts.ID = tr.object_id)
INNER JOIN staging_rnpl_term_taxonomy tt
ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
INNER JOIN staging_rnpl_postmeta AS mt3
ON ( staging_rnpl_posts.ID = mt3.post_id )
INNER JOIN staging_rnpl_postmeta AS mt4
ON ( staging_rnpl_posts.ID = mt4.post_id )
WHERE ( ( post_type = ‘event’
AND ( post_status = ‘publish’
OR post_status = ‘private’ ) ) )
AND staging_rnpl_posts.ID != 47
AND ( tt.term_id = 22 )
AND ( mt3.meta_key = ‘imic_event_start_dt’
AND ( ( mt4.meta_key = ‘imic_event_start_dt’
AND CAST(mt4.meta_value AS DATE) >= ‘2015-12-14’ ) ) )
GROUP BY staging_rnpl_posts.ID
ORDER BY mt3.meta_value ASC*******************************************
** **
** Related Posts by Taxonomy Debug **
** **
*******************************************type:
widget
——————————————-
cached:default
——————————————-
current post id:47
——————————————-
taxonomies used for related query:‘poslugujacy’
——————————————-
terms found for current post:Array
(
[0] => 22
)——————————————-
terms used for related query:Array
(
[0] => 22
)——————————————-
related post ids found:Array
(
)——————————————-
widget args:Array
(
[title] => Wydarzenia
[posts_per_page] => 5
[taxonomies] => poslugujacy
[post_types] => Array
(
[0] => event
)[format] => links
[image_size] => thumbnail
[columns] => 0
[singular_template] =>
[random] =>
[post_id] => 47
[caption] => post_title
)——————————————-
function args:Array
(
[post_types] => Array
(
[0] => event
)[posts_per_page] => 5
[order] => DESC
[fields] =>
[limit_posts] => -1
[limit_year] => 0
[limit_month] => 0
[orderby] => post_date
[exclude_terms] => Array
(
)[include_terms] => Array
(
)[exclude_posts] => Array
(
[0] => 47
)[post_thumbnail] =>
[related] => 1
)——————————————-
related posts query:SELECT posts.* , count(distinct tt.term_taxonomy_id) as termcount FROM posts INNER JOIN term_relationships tr ON (posts.ID = tr.object_id) INNER JOIN term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN postmeta AS mt3 ON ( posts.ID = mt3.post_id )
INNER JOIN postmeta AS mt4 ON ( posts.ID = mt4.post_id ) WHERE ( ( post_type = ‘event’ AND ( post_status = ‘publish’ OR post_status = ‘private’ ) ) ) AND posts.ID != 47 AND ( tt.term_id = 22 ) AND ( mt3.meta_key = ‘imic_event_start_dt’
AND ( ( mt4.meta_key = ‘imic_event_start_dt’ AND CAST(mt4.meta_value AS DATE) >= ‘2015-12-14’ ) ) ) GROUP BY posts.ID ORDER BY mt3.meta_value ASC——————————————-
requested template:related-posts-links.php
——————————————-
widget:Array
(
[name] => Staff Sidebar
[id] => sn431_273jkg
[description] =>
[class] =>
[before_widget] => <div id=”related-posts-by-taxonomy-3″ class=”widget related_posts_by_taxonomy”>(Debug Widget)
[after_widget] => </div>
[before_title] => <div class=”sidebar-widget-title”><h3 class=”widgettitle”>
[after_title] => </h3></div>
[widget_id] => related-posts-by-taxonomy-3
[widget_name] => Related Posts By Taxonomy
)——————————————-
OK. I found the problem. You used the same variable $events : both for comparision and array. And array cleared it ๐
Now it works correct ๐
Thank you. How can I send you my translated po file?$events = ‘events’;
$post_type = get_post_type( $post_id );
if ( $post_type !== $persons ) {
// Don’t return anything if the current post is not the ‘persons’ post type
return array();
}$events = array();
I’m glad you have got it resolved.
And thank you for the translation. You can send it to [email removed]
The topic ‘Help with plugin adaptation’ is closed to new replies.