• I want to fetch 5 post titles as a list, and they should be selected by ID.

    I know how to fetch one single post title, I do that like this:

    // The Query
    query_posts('p=52');
    
        while (have_posts()) : the_post();
    		the_title ();
    		echo "<br />";
    	endwhile;
    // Reset Query
    wp_reset_query();

    I would like to select 5 titles by 5 different ID’s like so:

    // The Query
    query_posts('p=52, 22, 32, 10, 16');
    
        while (have_posts()) : the_post();
    		the_title ();
    		echo "<br />";
    	endwhile;
    // Reset Query
    wp_reset_query();

    How can I accomplish that?

    It would be best if the ID’s were separated by a comma. Because they’re saved as a option in ONE row and I intend to fetch them, and then explode and implode them again separated by a comma.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Hi Chaseman,
    You can use the following code:

    // The Query
    query_posts('post__in' => array(52,22,32,10,16));
    
        while (have_posts()) : the_post();
    		the_title ();
    		echo "";
    	endwhile;
    // Reset Query
    wp_reset_query();

    Thread Starter chaseman

    (@chaseman)

    Nice one, thank you, I didn’t even know it works with an array.

    it is not even neccessary to run a query:

    https://codex.www.ads-software.com/Function_Reference/get_the_title

    $post_ids = array(52,22,32,10,16);
    foreach($post_ids as $post_id) {
    echo get_the_title($post_id) . '<br/>';
    }
    Thread Starter chaseman

    (@chaseman)

    I couldn’t get either one to work.

    vinay, with your version it will simply fetch all the titles from the database, but not the ones I selected with the id’s.

    alchymyth, with your version it will give me an error saying that I supplied a wrong argument for the foreach statement.

    In both cases I didn’t exactly have an array like in your examples I rather had variables like these ones:

    $blurb1_title = explode (" ", get_option ('blurb1_titles'));
    $blurb_title_ids = implode (", ", $blurb1_title);

    This may be the reason why it didn’t work for me. But I have to somehow fetch the id’s from the database and separate them with commas.

    the first explode will give you the array that you need; assuming that get_option ('blurb1_titles') contain post IDs separated by spaces.

    if they are separated by comma, the explode needs to be changed to:
    $blurb1_title = explode (",", get_option ('blurb1_titles'));

    this might work:

    $blurb1_title = explode (" ", get_option ('blurb1_titles'));
    foreach($blurb1_title as $post_id) {
    echo get_the_title($post_id) . '<br/>';
    }
    Thread Starter chaseman

    (@chaseman)

    To avoid confusion, yes they are separated by spaces.

    So are you saying, I should simply skip the part of imploding them and separating them by commas again? Just exploding is enough?

    Well I will try and see, but what you’re saying makes sense, because it is the explode that gives me the array with the variable. Not sure if I still have an array with the implode or if it’s just a string, never have tried it.

    Thread Starter chaseman

    (@chaseman)

    Ok I just tried it, and it worked GREAT! Thank you a lot alchymyth, I realize there was no need to implode it, simply exploding by the space is enough.

    Thread Starter chaseman

    (@chaseman)

    There’s only one problem with the foreach version, when the database row is EMPTY, meaning there is no entry to begin with, then it will list the first post (post_id=1) for some reason.

    $blurb_title = explode (" ", get_option ('blurb3_titles'));				
    
    if ($blurb_title != ' ') {
    include ($post_title_path);
    } else {
    
    }

    The foreach loop is inside the included file.

    I tried to solve this with an if statement and I’ve tried all numerous possible options, !empty, isset, etc. they all don’t work as I expect it.

    I don’t really understand why the variable is simply not considered as empty (I guess because of the explode).
    When I echo out the variable I simply get “Array”.

    EDIT:

    What I know is that it’s NOT equal empty ( == ‘ ‘), but at the same time I don’t know with what it’s filled either. : /

    do the test on the option field and try:

    if ( get_option ('blurb3_titles') != '' ) {
    
    $blurb_title = explode (" ", 				
    
    include ($post_title_path);
    } else {
    
    }
    Thread Starter chaseman

    (@chaseman)

    Thank you A LOT, that worked!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to Fetch Selected Post Titles by ID?’ is closed to new replies.