• Hi All,
    This seems like it should be easy, but I’ve been pulling my (thinning) hair out getting it to work. What it boils down to is, how do I get the content of a post from the wordpress database and replace the contents of an HTML <div> with that content?

    At page load time, I have php that generates the post ID for the corresponding image, i.e.:
    onmousedown="javascript:getDescription('<?php the_ID(); ?>');"

    That onmousedown() calls a javascript that passes the value of the post I want to query to a php file, i.e.:

    <script type="text/javascript">
                    function getDescription(for_id) {
                        $.ajax({
                            type: 'GET',
                            url: "<?php bloginfo('template_url'); ?>/assets/includes/get-description.php?id=" + for_id,
                            success: function(data, textStatus, jqXHR){
                                $('#textdescription').html(data);
                                 }
                            });
                    }
            </script>

    The php file get-description.php then should do the query and return the post contents, which the javascript then uses to update the <div> contents:

    <p><?php
    $contentVar = $_GET['id'];
    $post = get_post($contentVar);
    $content = $post->post_content;
    echo $title;
    ?></p>

    So this is all tested and working, except for the php file that is supposed to do the database query— I can’t get it to return anything when I query. I can return other content including my incoming post id, so I know all the pipes are connected and working.

    Any help appreciated, thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Ajax is a pain to troubleshoot. A couple things:

    1. Try using a different URL parameter than “id” — that’s sorta off-limits because WP uses that in some circumstances. It may not be a conflict, but it might be… best to avoid it, just in case.

    2. Check to ensure that the page is working all by itself. This isn’t strictly how it’s supposed to work with the WP Ajax API, but if you can get your page to work when you request it directly in your browser, e.g.
    http://yoursite.com/assets/includes/get-description.php?post_id=123

    then you’ll be able to troubleshoot the query your code on that page.

    You’ll have to put an include to the wp-load.php file to get WordPress up and running on that independent page, but if that page doesn’t work independently, there’s no way the AJAX version of it will work.

    3. Any time you use jQuery’s “html” method, you MUST wrap your output with html tags. If your page returns “xyz”, the .html() method won’t work. Instead, you have to wrap the output with some tag, e.g. “<p>xyz</p>”.

    Hope that helps.

    Thread Starter ohnoezmahblog

    (@ohnoezmahblog)

    Thanks for the pointers. I renamed the URL parameter to post_to_fetch, which didn’t have any effect that I could see, but I agree that it’s a better practice.

    Regarding #3, I believe the output is being wrapped in <p> tags from the php file, is that what you mean? The <p> tags are making it into the html source.

    I feel like there’s some kind of file I need to include or something, that is included in the standard php files loaded by WordPress. That post query code is straight out of the codex, I don’t think it’s the code per se.

    Since I’m able to pass information through the php file and back out to the html I feel like it’s not an AJAX problem.

    You can jump-start the entire WP application by including the wp-load.php file — you just gotta figure out where it is in relation to your file. Technically, it’s better to do the Ajax requests via the WP Ajax API, but that’s a lot more confusing in my opinion.

    Again, the single most important thing here is whether or not you can access that page directly in a browser, e.g.
    http://yoursite.com/assets/includes/get-description.php?post_to_fetch=123

    If hitting the page directly doesn’t work, then hitting it with Ajax won’t work. Period. Test the page directly first. Does that page work?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to query post content with javascript?’ is closed to new replies.