WordPress.org

Forums

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

  1. markgia
    Member
    Posted 4 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 );
    
    	$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

  2. keesiemeijer
    moderator
    Posted 4 years ago #

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

  3. markgia
    Member
    Posted 4 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
    moderator
    Posted 4 years ago #

    try apply_filters

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

Topic Closed

This topic has been closed to new replies.

About this Topic