Support

Account

Home Forums General Issues WP_query from Post Object

Solved

WP_query from Post Object

  • I have been looking for how to query post of the current single-employee.php who is has been select in a Post Object dropdown on the post page with the category of Publication.
    Below is what I have so far which get’s me all of the post with the category of Publication, but I only need the ones that are associated with the current employee of the page which is specified in a custom field of Author as author_field. We need to do it this way cause employees are not users and will not be entering their own publications. Any help would be great Thanks!

    <?php
     $wp_query = new WP_Query( array(
                            'post_type' => 'post',
                            'cat' => 10,
                            'meta_query' => array(
                            array(
                                'key' => 'author_field',
                                'value' => '// Need to get the name or value of the current page //' 
                            ),
                            'post_per_page' => 2
                        )); ?>
  • Hi @scottcarlton

    I will need to know a few more things:
    1. Where is the ‘author_field’ field located? Where is it’s data saved? What are it’s location rules?
    2. What type of field is ‘author_field’
    2. On what page / template are you running this code?

    Basically, I’m failing to understand how the author_field field is used in your wp-admin

    Thanks
    E

  • Thanks so much for your reply. I can give you more.
    I have a custom post type for employees. The employees have publications on the internet and the admin will be putting these into the system. They will select from a post object field that is set up to select from the custom post type employees. This selects the employee and list them as the author of the post under the category of “publication” as mentioned about with cat ID of 10.

    Now on the employees page you can select an employee. When you select an employee you are taken to their bio page (single-employees.php) where I am trying to do a 2 post wp_query of post that they have been selected as the author. I couldn’t use the default author field for this because employees are not users.

    the author is set up as a post object for the custom post employees as Field label “Author” and Field Name “author_field”.

    Below I have attached some screen shots. I hope this helps and thanks again.

  • Hi @scottcarlton

    Thanks for the info.

    So, the code what you are attempting to write will be run on the single-employees.php template. This template is run when the global $post is the post object of an employee.

    That means you can find the employee ID like so:

    $id = get_the_ID();

    then, you can find all publications of this employee like so:

    
    <?php
    
    $wp_query = new WP_Query( array(
        'post_type' => 'post',
        'meta_query' => array(
    	    array(
    	        'key' => 'author_field',
    	        'value' => get_the_ID()
    	    ),
        )
    ));
    
    ?>
    

    Does that help?

    Thanks
    E

  • Thanks so much, did’t realize it was that simple. All good! Only issue was a missing ) for the second array in meta_query.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘WP_query from Post Object’ is closed to new replies.