WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WP_Query in add/edit page (adding custom meta box) (5 posts)

  1. Daniel Gilfoy
    Member
    Posted 3 years ago #

    Hello all.

    I'm trying to create a meta-box on pages that pulles in all posts of a category (featured residents) that will hold an ID of the post which can later be pulled on that page in the sidebar.

    Pretty simple code, or so I thought. The code I link to below works, but replaces ALL of the post data (title, description, revisions) with the last post in the category I'm looking for.

    No idea why, I'm creating a new WP_Query. I've also tried setting a $tmp_wp_query = $wp_query and then re-doing it and setting $wp_query back to the temp holder when I'm done. Nothing has worked. I'm stumped. Am I going to have to write a custom SQL statement for this to work?

    Pastebin

  2. stvwlf
    Member
    Posted 3 years ago #

    I'm not following exactly what you are trying to do.

    I don't think this code is correct:

    $featured = new WP_Query(array("post_type"=>"post","category"=>1,"numberofposts"=>"999"));

    You can do it as
    $featured = new WP_Query('cat=1&posts_per_page=-1');

    You don't need to say "post_type = post" because only posts have categories so that is implied by cat=1. The syntax has always been cat=1 and not category=1. And its never been numberofposts, used to be showposts, is now posts_per_page. rather than 999 you can say -1 which means no limit to how many posts are returned.

    I'm not following exactly what you are doing....

    You are grabbing all the custom fields from the original post in an array, in get_post_custom. That is not a single value, its an array. Then inside the loop you are displaying posts if $custom is true. However you are comparing whether the original post's $custom is true, not the custom fields from each post the loop is pulling in. In other words the value of $custom never changes in the loop, so its either going to be true on every single post or false on every single post, depending on the status of the post whose custom fields you originally pulled from the database before the custom query loop. every post being returned in the loop. So $custom is going to either be true or false on every single post in the loop since you aren't looking at each post's custom fields, only the custom field of the original post. If you are testing for just one specific custom field you are better off using get_post_meta.

    The syntax for WP_query parameters is documented here
    http://codex.wordpress.org/Function_Reference/WP_Query

  3. Daniel Gilfoy
    Member
    Posted 3 years ago #

    Yes, I'm getting the original post's custom data.

    So in this example, I have the About page. When I edit the about page, I want a meta box that has all posts in the "featured residents" category and be able to select one. On the front end, when a person views the about page, they will see the person I selected (the post data which is about that person anyway). This data will actually be in the sidebar with a link to read more - with a link to the post itself.

    The custom meta information (the ID of the post I selected), is saved in the About page, under it's ID, under it's custom meta.

    I'll try changing my WP_Query to what you suggest, but I don't see how it is taking the information from the posts and filling the page data with it.

  4. Daniel Gilfoy
    Member
    Posted 3 years ago #

    Made a solution, still interested in a WP_Query solution if there was one. Making the changes as requested sill resulted in the PAGE title, content and revisions overwritten by the "last" Featured Resident category POST.

    Anyway, my solution works just fine, so I'll call it resolved. Feel free to offer suggestions as to why this happens. I'm completely stumped.

  5. stvwlf
    Member
    Posted 3 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic