The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

How to add filter on posts retrieved from WP_Query object? (4 posts)

  1. markgia
    Posted 5 years ago #

    The function below returns all posts within a custom taxonomy called 'course'.

    I have no problem retrieving the custom meta-data as shown in the loop, however, I would to add a filter to posts returned by this function, similar to 'the_content' filter for individual posts.

    function get_menu_items_by_course($course) {
    	$args['course'] = $course;
    	$posts = new WP_Query( $args );
    	foreach ($posts->posts as $post) {
    		$menu_items[]['item_name'] = $post->post_title;
    		$menu_items[]['description'] = $post->post_content;
    		$menu_items[]['menu_order'] = $post->menu_order;
    	//restore post global to current post in the main query
    	return $menu_items;

    Has anyone got any ideas on the best way to do this?


  2. keesiemeijer
    Posted 5 years ago #

    Can you explain a bit more. What kind of filter? What do you want it to do?

  3. markgia
    Posted 5 years ago #

    get_menu_items_by_course returns an array of posts and I would like to use an add_filter function on each of these posts before they are displayed on screen. If you are in the loop then you could use 'the_content' hook however I'm creating a custom loop so I cannot use this hook.

  4. keesiemeijer
    Posted 5 years ago #

    try apply_filters

    $mycontent = apply_filters( 'the_content', $mycontent );

Topic Closed

This topic has been closed to new replies.

About this Topic