Posted in: Customisation : Dynamic Content

Posted on:

Show Only Users Posts – Custom Query

If you're building a WordPress site and want to display posts or custom post types specific to the logged-in user, you're in the right place. In this tutorial, I'll walk you through how to set this up using Bricks Builder and a bit of custom PHP.

Why Filter Posts by Logged-In User?

This functionality is handy for creating personalised dashboards or front-end interfaces where users can manage their own content. For example:

  • A real estate site where agents can only see their own property listings.
  • A blogging platform where users can manage their own posts.
  • Any scenario where user-specific content visibility is required.

Prerequisites

Before we begin, ensure you have the following:

  1. Bricks Builder installed and activated.
  2. A basic understanding of WordPress custom post types (if applicable).
  3. Administrator access to your WordPress site.

Step 1: Enable Code Execution in Bricks Builder

To use custom PHP in Bricks Builder, you need to enable code execution. Here’s how:

  1. Navigate to Bricks > Settings in your WordPress dashboard.
  2. Go to the Custom Code tab.
  3. Enable the Code Execution option and restrict it to administrators (or a specific user if needed).
  4. Click Save Settings.
Code execution settings and user role permissions interface.

This step is crucial as it unlocks the ability to add custom PHP code directly within Bricks Builder.

Step 2: Set Up Your Template

Next, create or edit the template where you want to display the filtered posts.

  1. Open your template in Bricks Builder.
  2. Ensure you have a Loop added to display your posts.

Step 3: Customise the Query with PHP

Now, let’s modify the query to filter posts by the logged-in user.

  1. Select the Query function in your template.
  2. Open the Query Options panel.
  3. Enable the Query Editor (PHP) option. This allows you to override the default query with custom PHP.
PHP query code editor screenshot

Step 4: Add the Custom PHP Code

Here’s the PHP code you’ll use:

PHP
$current_user_id = get_current_user_id();
  return [
    'post_type' => 'property',
    'post_status' => 'publish',
        "orderby" => [
        "title" => "ASC"
    ],
    'posts_per_page' => '-1',
    'author' => $current_user_id,
  ];

Key Points:

  • $current_user_id: This is a custom variable that holds the ID of the logged-in user.
  • post_type: Replace 'property' with your custom post type slug or 'post' for standard WordPress posts.
  • posts_per_page: Set the number of posts to display. Use -1 to show all posts.
  • author: This ensures only posts created by the logged-in user are displayed.

If you need help with your custom post type slug, tools like Advanced Custom Fields (ACF) can help you identify it.

  1. Paste the code into the Query Editor.
  2. Click Sign Code to validate the PHP. This step ensures the code is secure and ready to execute.
  3. Save your changes.

Step 5: Test the Functionality

To confirm everything is working as expected:

  1. Save your template and view it on the front end.
  2. Log in as a user who has created posts.
  3. Verify that only the posts associated with the logged-in user are displayed.

For additional testing:

  • Use a plugin like User Switching to switch between users and check the filtered results quickly.

Example in Action

Let’s say you’re building a real estate site. When an agent logs in, they’ll only see their own property listings. If another agent logs in, they’ll see a completely different set of properties, ensuring each user’s personalised and secure experience.

Customisation Options

You can further customise the query to suit your needs:

  • Post Status: Change 'publish' to 'draft' or 'pending' if needed.
  • Order By: Use 'date', 'ID', or other fields to sort posts.
  • Posts Per Page: Adjust the number of posts displayed per page.

Troubleshooting

If the filtered posts aren’t displaying as expected:

  1. Double-check the custom post type slug in the PHP code.
  2. Ensure the logged-in user has created posts or custom post types.
  3. Verify that code execution is enabled in Bricks Builder settings.

Conclusion

With just a few steps and a small snippet of PHP, you can create a dynamic, user-specific post display in WordPress using Bricks Builder. This is perfect for building personalised dashboards or front-end interfaces.

Join The Community

Join over 14,000 WordPress enthusiasts and become part of a vibrant community.