0

I need to excute posts from 2 seprated keywords using s=keyword with get_posts, but only searching in titles. I found this answer.

I created a custom template to use it in single page. Then I used a merged get_posts in my answer using this

So, my custom template code is:

<?php /* Template Name: custom template posts */ ?>

<?php get_header(); ?>
<?php add_filter( 'posts_search', '__search_by_title_only', 500, 2 ); ?>
<?php
//first query
$blogposts = get_posts(array(
    's' => 'keyword1', 'showposts' => -1,
    ));
//second query
$authorposts = get_posts(array(
    's' => 'keyword2', 'showposts' => -1,
    ));
$mergedposts = array_merge( $blogposts, $authorposts ); //combine queries
$postids = array();
foreach( $mergedposts as $item ) {
$postids[]=$item->ID; //create a new query only of the post ids
}
$uniqueposts = array_unique($postids); //remove duplicate post ids
$mypostcount = count($posts);
$posts = get_posts(array(
            //new query of only the unique post ids on the merged queries from above
    'post__in' => $uniqueposts,
    'posts_per_page' => 12,
    'paged' => $paged,
    ));?>
<?php remove_filter( 'posts_search', '__search_by_title_only', 500 ); ?>
<div class="post-listing">
<?php foreach( $posts as $post ) : ?>
<?php setup_postdata($post); ?>
// layout code here
<?php endforeach; ?>
</div>
<?php query_posts('paged='.$paged.'&posts_per_page='. $cat_query); ?>
<?php if(function_exists('tie_pagenavi') ) { tie_pagenavi(); } ?>
<?php wp_reset_postdata();?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

When I go to wordpress admin, create new page change the default template for it, every thing is okay, but one problem my pagination showing a lot of pages which empty after the last page the number of all pages calculated from all posts in my database.

I am using a custom theme which has an internal pagenavi in it called tie_pagenavi(). I also tried the wp_pagenavi plugin.

Note: I am using $mypostcount = count($posts); to calculate the number of posts. I tested it by echo. It is really returned by the actual posts number in this loop but I am not sure how to use it to limit the pagination numbers.

You can check every thing by your self in http://elnhrda.com/e/sisi2posts/page/56/. 56 is the last page which has posts.

Edit: After searching and testing more, the problem is coming from this line

query_posts('paged='.$paged.'&posts_per_page='. $cat_query);

If I remove it the pagination disappears. If I add '&showposts=2000' to it the pages numbers changed the total number of my posts divided into 2000.

  • Your issue is with the use of query_posts(). Please use a proper custom query, and then modify your question accordingly. – Chip Bennett Oct 29 '13 at 12:16
  • i have used wp_query before but it isn't achieve my goal with merging ... please suggest a full solution – adnan Oct 29 '13 at 12:36
  • "please suggest a full solution" - replacing an improper use of query_posts() with a proper use of WP_Query() is a full solution. If you continue to have problems after implementing it, please update your question with your current code, and describe those issues you're still experiencing. – Chip Bennett Oct 29 '13 at 12:45
  • because i don't know what is the improper in query posts, so i don't know how to fix it, so i asked you for a full solution, & as i mentioned above i have some problems with wp_query in merging sorry for inconvenience – adnan Oct 29 '13 at 13:22
  • 1
    ok, sorry for this & i will take care about this in the future – adnan Oct 29 '13 at 17:04
1

I didn't load the plugin to check and I never use query_posts(), but perhaps tie_pagenavi() is depending on the value in the $paged global variable. Try adjusting that value before (or after) calling query_posts(). Here is the code to adjust it before calling query_posts().

if ( get_query_var( 'paged' ) )
    // On a paged page.
    $paged = get_query_var( 'paged' );

else
    $paged = 1;

query_posts( "paged={$paged}&posts_per_page={$cat_query}" );

if ( function_exists( 'tie_pagenavi' ) )
    tie_pagenavi();
  • at the top of my code if ( get_query_var( 'paged' ) ) $paged = get_query_var( 'paged' ); else $paged = 1; & after this line query_posts("paged={$paged}&posts_per_page={$cat_query}"); unfortunately the same thing – adnan Oct 29 '13 at 15:26
  • +1 perfect although is doesn't work but +1 is because as you mentioned you don't use query_posts although you tried to help, really appreciate this – adnan Oct 29 '13 at 15:40
0

after more than 15 hours testing & working

The Solution is

replace this line

query_posts('paged='.$paged.'&posts_per_page='. $cat_query);

with

query_posts(array('post__in' => $uniqueposts, 'posts_per_page' => 12,'paged' => $paged,));

The Problem is

this line only responsible for run the pagination so it running it with all number of posts in the site if i not specifying to it which posts i need

Big thanks to every one tried to help me & hope this answer helping some body in the future

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.