I'm stuck on loading page content by id (through a plugin). What I have is the following:
<?php $my_query = new WP_Query('page_id=39'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
Instead of entering the id "39" though, it needs to come from $user_set_value
I'm able to echo the id like this:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'user_set_value', true);
wp_reset_query();
?>
..but how could I go about echoing $user_set_value into the first snippet so it would get the id on the fly?
Many thanks!
I'm stuck on loading page content by id (through a plugin). What I have is the following:
<?php $my_query = new WP_Query('page_id=39'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
Instead of entering the id "39" though, it needs to come from $user_set_value
I'm able to echo the id like this:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'user_set_value', true);
wp_reset_query();
?>
..but how could I go about echoing $user_set_value into the first snippet so it would get the id on the fly?
Many thanks!
Share Improve this question asked Jan 24, 2015 at 19:26 RayRay 251 silver badge6 bronze badges2 Answers
Reset to default 0The values assigned to $user_set_value
should be stored somehow in some form, usually an option. You will know how the values and where the values are stored. It is easy then from there
$user_set_value = get_some_saved_option_value();
$args = array(
'page_id' => $user_set_value
);
$q = new WP_Query( $args );
Just change get_some_saved_option_value()
with the actual method to get the value.
u tried using:
global $post;
$postid = $post->ID;
the $post
contains data from the current post or page
EDIT: for more info see codex : http://codex.wordpress/Function_Reference/$post.
also i think i may have misunderstood your question, are u after:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
// get user value and assign it here
$user_set_value = get_post_meta($postid, 'user_set_value', true);
wp_reset_query(); ?>
// pass user value into query
<?php $my_query = new WP_Query('page_id='.$user_set_value); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>