I have a simple page structure like Page One, Page Two, Page Three.

Each page has tag of 'content'.

In the Wordpress loop how could I create an array for each page with the page title as the array name.

My code is like this.

        $mt_test_args = array(
            'post_type' => 'page',
            'tag' => 'content',
            'order' => 'ASC'

        $mt_test_loop = new WP_Query($mt_test_args);


                //Trying to create array here from page title.

            $arr_name = get_the_title();

                $arr_name = str_replace(' ','_',$arr_name);

                $arr_name = array();


    <option><?php the_title(); ?></option>

    <?php endwhile; endif; ?>

    <?php wp_reset_postdata(); ?>

This is demo code, the actual set up is a lot more complex

1 Answer 1


I think this is what you're looking for:

$$arr_name = array();

If $arr_name = 'post_title', then the above would mean:

$post_title = array();
  • The page titles have spaces that I need to replace with underscores then use that name to name the array Jul 8, 2013 at 18:47
  • you have that part... $arr_name = str_replace(' ','_',$arr_name); then replace the next line with what i gave you: $$arr_name = array(); and you'll have a PHP variable with the name of $what_ever_your_post_was_called. Is that what you want? Or do you want an array with all the names? Like, array('one_post_title', 'some_other_title', 'third_post_title')??? Jul 8, 2013 at 18:55
  • Thanks Mike, I want the first thing you posted. So what does the double $$ do? Jul 8, 2013 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.