Support » Fixing WordPress » Query a single private page with query_posts or wp_query

  • Hi everybody,

    I need to query a single page with query_posts (changing the main loop).

    I need to query its content in a single page, but I don’t want the page to be url queryable. So I created it as private.

    I modify the query this way:

    $args = array(
    	'pagename' => 'here_the_page_slug',
    	'posts_per_page' => 1,
    	'post_type' => 'page',
    	'post_status' => array( 'publish', 'private' ) // or just 'private'
    );
    
    query_posts( $args );

    But does’t work. I also tried the same query with a secondary loop using wp_query with no success.

    The only way I could get it was using get_page_by_path, but I need to modify the loop.

    Any idea?

    Thanks for your time in advance

Viewing 11 replies - 1 through 11 (of 11 total)
  • Or maybe there’s a better approach so as a page / custom post isn’t URL queryable?

    esmi

    (@esmi)

    Forum Moderator

    What do you mean by “I don’t want the page to be url queryable”?

    I mean, that I don’t want users to access the page directly by the url.

    For example, from:

    http://my_wp_site.com/page-slug

    esmi

    (@esmi)

    Forum Moderator

    If it’s a static page and you are using any sort of custom permalink structure, that simply isn’t possible.

    Hi esmi, thanks for your reply.

    What I need is to query with WP_Query a given private page by ID or slug, but it doesn’t work. My query is:

    $args = array(
    	'pagename' => 'here_the_page_slug',
    	'posts_per_page' => 1,
    	'post_type' => 'page',
    	'post_status' => array( 'publish', 'private' ) // or just 'private'
    );
    
    $query = new WP_Query( $args );
    esmi

    (@esmi)

    Forum Moderator

    That should work.

    It works if you’re logged in, but not for a normal visitor.

    And I need the page to be private. If I specify a “pagename” or ID, for not logged users it doesn’t retrieve anything.

    But for example, if I do a query like this:

    $args = array(
    	'posts_per_page' => 3,
    	'post_type'		 => 'page',
    	'orderby'		 => 'menu_order',
    	'order'			 => 'ASC',
    	'post_parent'	 => get_the_ID(),
    	'post_status'	 => array( 'publish', 'private' ) // children are private
    );

    It does also return the private pages.

    It’s a WP core bug?

    esmi

    (@esmi)

    Forum Moderator

    Try using:

    $args = array(
    	'pagename' => 'here_the_page_slug',
    	'posts_per_page' => 1,
    	'post_type' => 'page',
    	'post_status' => array( 'private' )
    );
    
    $query = new WP_Query( $args );

    The same. It works when I’m logged in but doesn’t when I’m not.

    esmi

    (@esmi)

    Forum Moderator

    Then the issue must be in your placement of the query. There’s nothing wrong with the query itself.

    lemmonaid

    (@lemmonaid)

    Hi,

    What do you mean by placement of the query?

    I have the same issue but with posts:

    $queryArgs = array(
     'post_status' => array( 'publish', 'private' ),
     'name'        => 'post-slug',
     'tax_query'   => array( array(
      'taxonomy' => 'post_format',
      'field'    => 'slug',
      'terms'    => 'post-format-quote',
      ) )
    );
    $testimonials = new WP_Query( $queryArgs );

    This works pulls the post when you are logged in, but not when you’re logged out…

    Thanks,

    Oliver

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Query a single private page with query_posts or wp_query’ is closed to new replies.