Support » Fixing WordPress » query_posts for custom post type using post id (or name)

  • Resolved jnhghy

    (@jnhghy)


    Hi,
    I need to create an option for my users to save posts as favorites, I searched for plugins but none of theme are able to do this because I am using a custom post (‘ad_listing’). This creates issues approching the database because approaching the query from the wp_posts point of view table will have issues with the post_type as I am using custom post type “ad_listing” and approaching the query from wp_term_taxonomy point of view will create issues because the taxonomy is custom (“ad_cat”) insted of the default category and I didn’t saw a linking way.

    So this being said let me give some exaples of the code that will show exacly what I am tring to do but didn’t reach the correct output:

    query_posts('post_type=ad_listing&p=160');
    -this query will display all the posts in “ad_listing” so using “p=160” has no effect;

    query_posts(array('name'=>'b8d7p','post_type' => 'ad_listing'));
    -this query will display all the posts in “ad_listing” so using “‘name’=>’b8d7p'” has no effect;

    query_posts(array('post_type'=>'ad_listing','post__in' => array(160,159)));
    -this query will display all the posts in “ad_listing”

    So this is my challenge I hope somebody will be up to the task at least for pointing me to the right direction.Thanks and
    Regards

Viewing 15 replies - 1 through 15 (of 17 total)
  • Thread Starter jnhghy

    (@jnhghy)

    So braking my head with this I end up with a not elegant/to use solution
    I can query all the posts and in the loop I can compare the post id with the id saved as the favorite post id. It is a solution but theres no way I can implement this, there has to be a way to query the database for a specific custom post id.
    Any suggestion will be highly appreciated.
    Regards

    Thread Starter jnhghy

    (@jnhghy)

    I really need help with these 🙂
    anybody that needed this type of code that can help me?
    or at least say that is not possible? – don’t really want to hear this but want to know :).

    User means, not site admin.. but a normal user ??

    Thread Starter jnhghy

    (@jnhghy)

    Yes, the site visitors needs to be able to save posts as their favorites and a “Favorites” button will take them to a page that will loop and display their favorites as post block with permlinks to the post

    Well i m not very good in explaining the things but i will try my best to explain my approach on this……

    I will use a rating plugin (lets say GD Star Rating) which will allow users to rate that Ad…

    Then I will create a custom template which will user a query that go to the database and will SELECT all the ads which are rated by the CURRENT LOGGED IN USER.
    This will provide user the list of All the Ads which the he have rated…
    among them i will use only those ads which are rate 10/10 by the user (which somehow indicates that the user liked that Ad.)

    And once you have the Ids of all the posts which the current user have rated 10/10.. you will be able to display the Title, Thumbnail and description of those posts using Loop…

    Hope it was not that confusing, as i told before that explaining is not one of my best attributes 🙂

    Thanks

    Thread Starter jnhghy

    (@jnhghy)

    Thanks for your time on this,
    The ideea is good but, I have no issue in getting the post id that the user want’s to save,
    The issue is querying in the loop for the Title, Thumbnail and description using the loop.
    Especially callyin the loop for the post id’s as the post is not a “post” or “page” and is a “ad_listing” and calling by id had created issues.
    Also any available plugin will not work from the same cause “ad_listing” post type. The plugins will search for “posts” and will have no effect on my ads.
    So? where do we get from here? 🙂

    What theme are you using… Is it the Classipress from the Appthemes ?
    And as far is I believe, Ad listings should also be treated as post… You can verify this by going to the Database.. and search the Ad Listing ID in Post table.. You should find your Ad Listings in wp_posts table..
    Thanks

    Thread Starter jnhghy

    (@jnhghy)

    As I said in my first post here I tried to get the query by using:
    query_posts('post_type=ad_listing&p=160');
    -this query will display all the posts in “ad_listing” so using “p=160” has no effect;

    query_posts(array('name'=>'b8d7p','post_type' => 'ad_listing'));
    -this query will display all the posts in “ad_listing” so using “‘name’=>’b8d7p'” has no effect;

    query_posts(array('post_type'=>'ad_listing','post__in' => array(160,159)));
    -none worked while for category querys and custom taxonomy querys it works, I played for 2 days now with this and this shwos from my point of view that ad listings are not treated as posts at least when you are using in the query the “p=$ad_id”;.
    Yes is classipress from apptheme or let’s say it was… it was because I modified it a lot to fit my needs.

    I am working on a project in which we want to show the user all the posts which he have rated.. and for that i have written a code which i would like to share with you here…

    // First get the logged in user ID
    
                                <?php
        wp_get_current_user();
        $user_id= $current_user->ID;
    
        ?>
    //Now Get the ID of the POSTS which logged in user have rated
    
          <?php
    
     $query = mysql_query("SELECT * FROM wp_gdsr_votes_log WHERE vote_type LIKE 'article' AND user_id=$user_id ");
    //Above query will take all the posts which the usr have rated
    	$numrows = mysql_num_rows($query);
        		if ($numrows!=0){    ?> 	<div class="tj_wrapper"><ul class="tj_gallery"> <?php
        while ($row = mysql_fetch_assoc($query)){
    
    // Below Line will Extract IDS of rated POSTS in a loop
    
    	$the_post = $row['id'];
             ?>
    
        //Below Code will Use the ID and will display the Thumbnail and TITLE with the permalink of rated post
    
    <li><a href="<?php echo get_permalink( $the_post ); ?>"><?php
    echo get_the_post_thumbnail($the_post, 'thumbnail'); ?>   </a> <br />
    <?php echo get_the_title($the_post); ?>
    </li>
    
    <?php } ?> </ul></div><?php }
            ?>
    Thread Starter jnhghy

    (@jnhghy)

    I see, so you fetch the posts without using wordpress query_posts, I am trying to avoid this as I think it will slow down the site and also it means that I will have to write more code. But this is by far the best way to do it at this point.
    Thenks

    yes.. and i use this as the final option 🙂
    BTW i also use Classipress in one of my sites.. and i have seen that the ads can be found in wp_posts table 🙂

    Thread Starter jnhghy

    (@jnhghy)

    yes … that’s correct but if you go in that table and check post_type you will is that ads have the post_type “ad_listing” and this impacts in the query_posts function.

    post_type will help you to sort only the Ads in the loop
    WHERE post_type LIKE 'ad_listing' 😀

    Thread Starter jnhghy

    (@jnhghy)

    As I said I runed the querys and didn’t got the right info from the database so I see you run a lot of mysql querys throw php I don’t want to do that I am trying to accomplish this using query_posts so it’s a nother thing I understood that I can use a php function to display my posts but this means I cannot use the loop file to display them and that I need to create custom output.

    At special I want to be able to use
    the_post();
    function that is why I’m trying to use query_posts 🙂

    ok.. let me know if you figure this one our with query_posts..
    I would also love to avoid custom queries 😀
    Thanks

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘query_posts for custom post type using post id (or name)’ is closed to new replies.