WordPress.org

Ready to get started?Download WordPress

Forums

Load posts from external source if not found in database (4 posts)

  1. bilaljshahid
    Member
    Posted 2 years ago #

    I want to modify WordPress so that it requests content from a function if content is not found in the database.

    I want to edit the section of the code which requests and returns the post content from the database so that: if it does not find the content in the database, it calls and gets the content from my function, instead of returning null/not found. I am not sure where this section of the code is or how to go about changing it.

    I can write a set of functions which return the title/body/excerpt/tags/etc... when give the post id not found in the database. Please help me in finding who/where to make this edit.

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    you could call a function if posts are not found.... that's what I do in my theme.

    <?php if ( have_posts() ) : ?>
    
    	<?php while ( have_posts() ) : the_post(); ?>
    
    Your loop
    
    	<?php endwhile; ?>
    
    	<?php else : ?>
    
    		<?php voodoo_else(); ?>
    
    <?php endif; ?>

    So if posts exist, they are displayed by the loop, otherwise voodoo_else is run

  3. bilaljshahid
    Member
    Posted 2 years ago #

    That would work, but I want to implement in such a way that the rest of the wordpress code treats it as a regular post. I was thinking that I could just change the part that reads the posts from the database. I would imagine that there are functions like: getPostFromID($postID), getPostFromTitle($postTitle).. that query the database and return the "post object." I would prefer to just change these functions so that, as far as the the rest of code is concerned, it is being taken from the database. What I am trying/hoping to do is to "encapsulate" the database from the rest of the WordPress code.

  4. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    well, if you just want to grab a real post from the database, then yeah, also simple.

    You can just run a secondary loop for your :else: to grab a specific post if no posts are found by the original query/loop

    http://codex.wordpress.org/The_Loop#Multiple_Loops
    http://codex.wordpress.org/Function_Reference/query_posts
    http://codex.wordpress.org/Class_Reference/WP_Query

    There a variety of info to get you rollin down the road. BUt you can most certainly query / get a post by ID, or any other parameter really

Topic Closed

This topic has been closed to new replies.

About this Topic