Support » Fixing WordPress » How to add filter on posts retrieved from WP_Query object?

  • markgia

    (@markgia)


    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 );
    
    	$menu_items;
    	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
    	wp_reset_postdata();
    
    	return $menu_items;
    }

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

    Cheers,
    Mark

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    moderator

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

    markgia

    (@markgia)

    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.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    try apply_filters

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add filter on posts retrieved from WP_Query object?’ is closed to new replies.