• Ok – So at this point I’m just working at the mysql level
    Which I’m ok on – but it’s when you throw joins and stuff into them mix and multi dimensionsal arrays it gets a bit crazy fairly quickly!

    I need a hand as we are looking thru a wordpress database and trying to get a list of post’s that do not have any tags assigned to them

    Is there a way to do this

    I’m thinking some sort of mysql join ?

    Any help greatly appreciated

    Steve

Viewing 1 replies (of 1 total)
  • Moderator Marius L. J.

    (@clorith)

    HI,

    I modified your title a little as the original one didn’t make much sense ??

    The following will get any post without a tag, it’s specifically tuned towards posts and tags, but can also be adjusted to deal with pages (or any CPT, or just all content altogether) as well as categories instead of terms.

    global $wpdb;
    
    $entries = $wpdb->get_results( $wpdb->prepare( "
    	SELECT
    		p.ID,
    		t.term_id
    	FROM
    		" . $wpdb->posts . " p
    	LEFT JOIN
    		(
    			SELECT
    				term_rel.object_id,
    				term_rel.term_taxonomy_id,
    				t.taxonomy,
    				t.term_id
    			FROM
    				" . $wpdb->term_relationships . " term_rel
    			JOIN
    				" . $wpdb->term_taxonomy . " t
    					ON ( t.term_taxonomy_id = term_rel.term_taxonomy_id )
    			WHERE
    				t.taxonomy = %s
    			GROUP BY
    				term_rel.object_id
    		) t
    		ON ( t.object_id = p.ID )
    	WHERE
    		t.term_id IS NULL
    	AND
    		p.post_type = %s
    ", 'post_tag', 'post' ) );

    What I did here is to just pull the ID number of the posts, but I only include posts where the term_id is NULL (empty).

    I check the term_id value by running a sub-query which grabs any post_tag entry and group them based on post ID (referenced as object_id by the database) and then I only pull out the row that has an object_id that matches our current post id (the ON ( t.object_id = p.ID ) block)

Viewing 1 replies (of 1 total)
  • The topic ‘Get posts with no tags assigned to them’ is closed to new replies.