Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
971 views
in Q2A Core by
reopened by

I have two Q2A installations on the same database.

The default posts are stored in qa_posts table.

The second installation posts are stored in new_posts table.

Is there a way to display a list of recent new_posts questions as a widget on the main Q2A site?

Currently I'm using feed tricks, but are there some "native" ways to do so?

Q2A version: 1.8.5
by
You can make a new query and change the prefix.
by
edited by
+1
Thank you.

I'm not sure I'm doing this right. I hope somebody can comment:


$PostArray = qa_db_read_all_assoc(qa_db_query_sub('SELECT postid, title, content, created
                                FROM `new_posts`
                                WHERE `type`="Q" ORDER BY created DESC
                                LIMIT 20
                                ')
                        );


foreach($PostArray as $post)
{

$this->output('<h1>'.qa_html($post['title']).'</h1>');
$this->output('<p>'.qa_html($post['content']).'</p>');
$this->output('<span>Posted on'.$post['created'].'</span>');

}
by
Well, you're on the right way.But I think, also there's traditional way of Q2A  to show a bunch of question via a plugin..Just you have to throw the question array as ['q_list']['qs'] key to the theme class.You can find more documentation here.https://docs.question2answer.org/code/structure/
by
+2
Can you post the solution as an answer instead of just closing questions? This is a Q&A script after all...

1 Answer

+1 vote
by

The problem with ['q_list']['qs'] approach or qa_any_to_q_html_fields () function is that it may return the WRONG question urls due to the mismatch between the second table and the core variables.

So, my solution is to mimic how that function list questions.

Here's my solution and it is used in a widget. You can customize your own way, for example like add pagination or add author names, shorten post content strings, etc...

function output_widget($region, $place, $themeobject, $template, $request, $qa_content)
    {


$PostArray = qa_db_read_all_assoc(qa_db_query_sub('SELECT postid, title, content, created
                                FROM `new_posts`
                                WHERE `type`="Q" ORDER BY created DESC
                                LIMIT 20
                                ')
                        );


foreach($PostArray as $post)
{
$link = 'https://mywebsite.com/secondinstallation'.str_replace(".","",qa_q_path($post['postid'],$post['title'],false));
echo '<div class="qa-q-list-item"><h1><a href="'.$link.'">'.$post['title'].'</a></h1>';
echo '<p>'.$post['content'].'</p>';
echo '<span>Posted on '.$post['created'].'</span></div>';


}


    }

by
To be clear,

If you use in a widget, then you output with PHP "echo" function.

If you use in a theme, you use $this>output

And if you create a custom page, you use $qa_content ['q_list']['qs']  or $qa_content['custom']
...