Running the loop inside a PHP function
-
I’ve seen various questions asked in this vein but nothing definitively about just having the loop accessible from a function. I have a very good reason to want the loop in a function–essentially as a lookup from a posted form. Simple.
This is what I have:
global $post; global $the_query; function myFunction(){ $args = array( 'category_name' => 'my-category-name', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => '-1' ); $the_query = new WP_Query( $args ); //Show posts if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) : $the_query->the_post(); $myPosts .= get_post_meta($post->ID, 'mycustomfieldname', true); endwhile; } return $myPosts; }
This is before I’ve even added any vars to the function call; all I get right now is one line of all the titles of each record (seemingly concatenated) and then a bunch of empty lines.
Do I have a glaring syntax error or am I going about the function all wrong?
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Running the loop inside a PHP function’ is closed to new replies.