WordPress.org

Ready to get started?Download WordPress

Forums

Query a single private page with query_posts or wp_query (12 posts)

  1. jtost
    Member
    Posted 1 year ago #

    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

  2. jtost
    Member
    Posted 1 year ago #

    Or maybe there's a better approach so as a page / custom post isn't URL queryable?

  3. esmi
    Forum Moderator
    Posted 1 year ago #

    What do you mean by "I don't want the page to be url queryable"?

  4. jtost
    Member
    Posted 1 year ago #

    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

  5. esmi
    Forum Moderator
    Posted 1 year ago #

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

  6. jtost
    Member
    Posted 1 year ago #

    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 );
  7. esmi
    Forum Moderator
    Posted 1 year ago #

    That should work.

  8. jtost
    Member
    Posted 1 year ago #

    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?

  9. esmi
    Forum Moderator
    Posted 1 year ago #

    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 );
  10. jtost
    Member
    Posted 1 year ago #

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

  11. esmi
    Forum Moderator
    Posted 1 year ago #

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

  12. lemmonaid
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic