WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problem with add_filter function (3 posts)

  1. ShArDiCk
    Member
    Posted 6 years ago #

    Hi,

    i was playing with add_filter to create a plugin that permit to list my posts in an mandatory order (date independent).

    So, i'm adding this filter

    add_filter('posts_join', 'wp_postorder_join');

    function wp_postorder_join() {
    global $wpdb;
    return " LEFT JOIN " . $wpdb->postmeta . " as postorder on postorder.post_id = " . $wpdb->posts . ".ID and postorder.meta_key = 'post_order' ";
    }

    Everything works fine but when i try to list posts by taxonomy item, the join settep up by wordpress creating the query is replaced by my join and query runs in error.

    I dont know if this is a bug or not but i have solved like this:

    at wp-includes\query.php line 1185

    $where = $where . apply_filters('posts_where', $where);
    $join = $join . apply_filters('posts_join', $join);

    So, i can mantain original join and adding a custom join.

  2. Kafkaesqui

    Posted 6 years ago #

    There is no problem here. The 'proper' way to go about this is to design your plugin like so:

    function wp_postorder_join($join) {
    	global $wpdb;
    	$join .= " LEFT JOIN " . $wpdb->postmeta . " AS postorder ON postorder.post_id = " . $wpdb->posts . ".ID AND postorder.meta_key = 'post_order' ";
    	return $join;
    }
  3. ShArDiCk
    Member
    Posted 6 years ago #

    very nice!

    thanx a lot!

Topic Closed

This topic has been closed to new replies.

About this Topic