Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Fotis K

    (@f_kalafatishotmailcom)

    After some playing around with custom queries I got it to work-Here’s my code to display PDF files as if they were ordinary posts (sorry for the b-quote but the code tag didn’t work properly):

    The query:

    global $wpdb;
    global $post;
    /*Creating the custom SQL query. Your code in the quotes*/
    $querystr = “SELECT DISTINCT wposts.*
    FROM $wpdb->posts wposts
    LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
    LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE
    (wpostmeta.meta_key=’Author’)
    and
    (
    ( $wpdb->term_taxonomy.taxonomy = ‘category’
    or
    $wpdb->term_taxonomy.taxonomy = ‘media_category’
    )
    and $wpdb->term_taxonomy.term_id IN(15)
    )
    and
    (
    (
    (wposts.post_type = ‘post’) and (wposts.post_status=’publish’)
    )
    or
    (
    (wposts.post_type = ‘attachment’) and (wposts.post_mime_type = ‘application/pdf’) and (wposts.post_status=’inherit’)
    )
    )

    ORDER BY LOWER(wposts.post_modified) DESC”;
    /*Execute the query and get the results*/
    $pageposts = $wpdb->get_results($querystr, OBJECT);

    the Loop(Don’t know how to make pagination work though)

    foreach ($pageposts as $post) {
    setup_postdata($post);
    if($post->post_type==’attachment’) {
    /* display a custom entry for the PDF file, where clicking takes you to the actual PDF not the page of it, since you hyperlink directly the attachment’s url not the post url*/
    echo ‘ID).'”>’;
    echo $post->post_title;
    echo ‘
    ‘; }
    else if ($post->post_type == ‘post’){
    /*treat simple posts that have proper html content differently*/
    $permalink = get_permalink($post->ID);
    $mytitle=the_title(”,”,false);
    echo ‘‘.$mytitle.’‘; }
    }

    Thread Starter Fotis K

    (@f_kalafatishotmailcom)

    I guess asking the post_parent to be NULL was the problem. My final working code(searching posts and media) is the following and should be placed in functions.php:

    function SearchFilter($query) {
      if ($query->is_search) {
        $query->set('post_type', array('attachment','post','page'));
        $query->set('post_status', array('publish','inherit'));
      }
      return $query;
    }
    add_action('pre_get_posts','SearchFilter');
    Thread Starter Fotis K

    (@f_kalafatishotmailcom)

    Ok, I got it working with the following query:

    global $wpdb;
    $querystr = "SELECT DISTINCT wposts.*
    		FROM $wpdb->posts wposts
    		LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
    		LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
    		LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    		WHERE
    			(wpostmeta.meta_key='Author')
    			and
    			(
    				(	$wpdb->term_taxonomy.taxonomy = 'category'
    					or
    					$wpdb->term_taxonomy.taxonomy = 'media_category'
    				)
    				and $wpdb->term_taxonomy.term_id IN(15)
    			)
    			and
    			(
    				(
    					(wposts.post_type = 'post') and (wposts.post_status='publish')
    				)
    				or
    				(
    					(wposts.post_type = 'attachment') and (wposts.post_mime_type = 'application/pdf') and (wposts.post_status='inherit')
    				)
    			)
    
    		      ORDER BY LOWER(wposts.post_modified) DESC";
    
    	$pageposts = $wpdb->get_results($querystr, OBJECT);
    Thread Starter Fotis K

    (@f_kalafatishotmailcom)

    Thanks for answering! Unfortunately TinyMCE is not an option, since its support for visually impaired people is very limited. As for switching from Visual to the HTML tab and setting breaks over there, it works until I switch back to Visual or reopen the document for editing (not to mention that I’m not the only article Author in the website – so forcing other people write HTML is not very practical)

    Thread Starter Fotis K

    (@f_kalafatishotmailcom)

    Yes certainly. I got the impression that it would be possilbe to have the PDFs listed along with the regular posts and whenever somebody in a category view clicks on a PDF “title” instead of a Post title, it would be the same to clicking on a link to this PDF file: If the browser has a plugin (like Chrome) it would open it in place, otherwise offer to download it!
    In reality, I’m trying to to find a way to to kill the overhead of adding a post for every file that merely links to it, both for the simpification of downloading/opening the article as well as the helping the authors in the article publishing.
    If it isn’t possible, do you know anyplug-in that might be able to help towards that direction?

    Thanks again for the effort and time to help!

Viewing 5 replies - 1 through 5 (of 5 total)