• I’m attempting to use the posts_join, posts_fields & posts_orderby filters with a query generated using get_connected()


    add_filter('posts_fields' , 'cnc_posts_fields', 11, 1);
    $connected = p2p_type('cast_crew_to_show')->get_connected($show_id,
    array(
    'posts_per_page' => -1,
    'connected_meta' => array('type' => $type),
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_key' => 'via_last_name'
    )
    );
    remove_filter('posts_fields' , 'cnc_posts_fields', 11, 1);


    function cnc_posts_fields($fields) {
    return $fields . ', 1 AS test';
    }

    This will generate the following SQL query


    SELECT wp_via_posts . * , 1 AS test, wp_via_p2p . *
    FROM wp_via_posts
    INNER JOIN wp_via_postmeta ON ( wp_via_posts.ID = wp_via_postmeta.post_id )
    INNER JOIN wp_via_p2p
    INNER JOIN wp_via_p2pmeta ON ( wp_via_p2p.p2p_id = wp_via_p2pmeta.p2p_id )
    WHERE 1 =1
    AND wp_via_posts.post_type = 'atl_cast_crew'
    AND (
    wp_via_posts.post_status = 'publish'
    )
    AND (
    wp_via_postmeta.meta_key = 'via_last_name'
    )
    AND (
    wp_via_p2p.p2p_type = 'cast_crew_to_show'
    AND wp_via_posts.ID = wp_via_p2p.p2p_from
    AND wp_via_p2p.p2p_to
    IN (
    SELECT wp_via_posts.ID, 1 AS test
    FROM wp_via_posts
    WHERE 1 =1
    AND wp_via_posts.ID
    IN ( 13467 )
    AND wp_via_posts.post_type = 'atl_show'
    AND (
    wp_via_posts.post_status = 'publish'
    )
    ORDER BY wp_via_posts.post_date DESC
    )
    )
    AND (
    (
    wp_via_p2pmeta.meta_key = 'type'
    AND CAST( wp_via_p2pmeta.meta_value AS CHAR ) = 'Cast'
    )
    )
    GROUP BY wp_via_posts.ID
    ORDER BY wp_via_postmeta.meta_value ASC

    The problem is the filter is being applied to both the main query and the subquery. I only need it on the main query.

    https://www.ads-software.com/plugins/posts-to-posts/

  • The topic ‘Using WP_Query filters with get_connected()’ is closed to new replies.