• Ghidra99

    (@ghidra99)


    I’m trying to figure out a way to be able to display a category’s name or slug by just using a post ID.

    Currently a plugin I’m using does a search for certain posts that contain event meta values. I’d like to be able to display the related category information (name, slug, etc.) for each event

    $where_category_clause = (0 == $category) ? '' :
    'AND cats.term_taxonomy_id =
    '.$wpdb->escape(stripslashes($category));
    
    	SELECT DISTINCT
    		meta.meta_value as 'date',
    		post.post_title as 'title',
    		post.post_content as 'fulltext',
    		post.ID as id,
    		post.post_excerpt as 'excerpt'
    	FROM
    		{$wpdb->postmeta} as meta,
    		{$wpdb->posts} as post,
    		{$wpdb->term_relationships} as cats
    	WHERE
    		meta.post_id = post.ID
    	AND
    		post.ID = cats.object_id
    	AND
    		meta.meta_key = 'rs_event'
    	AND
    		post.post_date <= '".current_time('mysql')."'
    	AND
    		meta.meta_value >= {$lower_time}
    	AND
    		meta.meta_value <= {$upper_time}
    	AND
    		post.post_status = 'publish'
    	{$where_category_clause}
    	ORDER BY
    		meta.meta_value {$sort_order}

    I’m assuming I need to make a seperate query after I’ve retrieved the event (post) ID, but I’m not sure how to go about calling each event’s category. I’m sure it involves JOIN-ing tables, but I’m really unfamiliar with how that works.

  • The topic ‘Specifying a Category From a Post ID’ is closed to new replies.